更新Javascript对象值为null

时间:2015-08-24 13:59:15

标签: javascript angularjs object updating

我试图添加一个'观看'每次有人访问该页面时,通过递增值来在我的页面上编号。

我有一个这样的对象:

{
    _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."
}

我通过AngularJS' .one()'检索此对象。方法:

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

目标是增加此值。目前我这样做:

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

但这不起作用,只给我一个空值,而这确实有效:

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

我做错了什么?

2 个答案:

答案 0 :(得分: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."
}

答案 1 :(得分:0)

我这样修好了:

我实际上并没有像Marc建议的那样更新实际对象

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