2015-08-24 43 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); 
});