0

I'm trying to add a 'views' number on my page by incrementing the value every time someone visits the page.

I have a object like this:

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

I'm retrieving this object via AngularJS's '.one()' method:

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

The goal is to increment this value. At the moment I'm doing it like this:

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

But that doesn't work and just gives me a null value, while this does work:

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

What am I doing wrong?

4
  • try movie.views = movie.views + 1; Commented Aug 24, 2015 at 14:00
  • Is it possible that you're not actually retrieving anything in the movie object? That would explain why direct assignment of a number works, but incrementing (a null) doesn't. Commented Aug 24, 2015 at 14:04
  • Try incrementing other variables of your movie, if that doesnt work too, movie doesnt get loaded so everything is undefined and incrementing doesnt work but setting it to 10 does. Commented Aug 24, 2015 at 14:05
  • Wait, that's an object from some database, not a plain JS one, right? Which DB (wrapper) are you using? Commented Aug 24, 2015 at 14:07

2 Answers 2

1

You can't increment a null value, set it 0 (zero) by default.

{
    _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."
}
Sign up to request clarification or add additional context in comments.

3 Comments

Sorry, that was a mistake in the question. I actually set is to 0. I updated the question.
Oh ... when doing a console.log(movie) what do you have as result and what is the views value?
{ "_id": "55da04e2fe08e73168fc7aea", "route": "movie", "reqParams": null, "restangularized": true, "fromServer": false, "parentResource": null, "restangularCollection": false }
0

I fixed it like this:

I wasn't actually updating the actual object like Marc suggested

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.