2015-08-24 60 views
0

我試圖通過在每次訪問該頁面時增加值來在頁面上添加「視圖」編號。通過AngularJS的 '一()'正在更新Javascript對象值null

{ 
    _id: "55da051afe08e73168fc7aeb", 
    url: "https://www.youtube.com/watch?v=eUdM9vrCbow", 
    title: "Django Unchained", 
    embedUrl: "https://www.youtube.com/embed/eUdM9vrCbow", 
    __v: 0, 
    downvotes: 0, 
    upvotes: 0, 
    views: 0, 
    created_on: "2015-08-24T13:37:33.951Z", 
    desc: "With the help of his mentor, a slave-turned-bounty hunter sets out to rescue his wife from a brutal Mississippi plantation owner." 
} 

我檢索此對象的方法:

我有這樣的對象

var movie = Movie.one($routeParams.id); 

我們的目標是增加該值。目前,我正在做這樣的:

movie.views++; 
movie.put(); 

但是,這並不工作,只是給了我一個空值,而這樣做的工作:

movie.views = 10; 
movie.put(); 

我在做什麼錯?

+0

試'movie.views = movie.views + 1;' –

+0

已嘗試過 – Roemerdt

+0

是否有可能沒有實際檢索「電影」對象中的任何內容?這可以解釋爲什麼數字的直接分配有效,但遞增(空)不會。 – Marc

回答

1

您無法增加null的值,默認情況下將其設置爲0(零)。

{ 
    _id: "55da051afe08e73168fc7aeb", 
    url: "https://www.youtube.com/watch?v=eUdM9vrCbow", 
    title: "Django Unchained", 
    embedUrl: "https://www.youtube.com/embed/eUdM9vrCbow", 
    __v: 0, 
    downvotes: 0, 
    upvotes: 0, 
    views: 0, //instead of 'null' 
    created_on: "2015-08-24T13:37:33.951Z", 
    desc: "With the help of his mentor, a slave-turned-bounty hunter sets out to rescue his wife from a brutal Mississippi plantation owner." 
} 
+0

對不起,這是一個錯誤的問題。我實際設置爲0.我更新了問題。 – Roemerdt

+0

哦......做一個'console.log(電影)'時你有什麼結果,什麼是'views'值? –

+0

{ 「_id」: 「55da04e2fe08e73168fc7aea」, 「路線」: 「電影」, 「reqParams」:空, 「restangularized」:真實, 「fromServer」:假的, 「parentResource」:空, 「 restangularCollection「:false } – Roemerdt

0

我固定它是這樣的:

我沒有實際更新像Marc實際對象建議

Movie.one($routeParams.id).get().then(function(response){ 
    response.views++; 
    $http.put("http://localhost:3000/movie/" + $routeParams.id, response); 
});