Mongodb update/$set only works every other time function is called- and returns [object Object] [duplicate]












0
















This question already has an answer here:




  • How to increment a field in mongodb?

    1 answer



  • Update And Return Document In Mongodb

    4 answers



  • what does [object Object] mean?

    7 answers




I have a movie review website. I have two collections in my mongo db, one for my movie reviews, and one for reviews submitted by users.



Whenever a user submits their own review, I want to increment the variable userReviewCount within the collection of my reviews.



I'm using mongoose and running everything within javascript:



function saveUserReview(review) {

collections.ReviewModel.findOne(
{ 'filmId': review._filmId.toNumber() },
function(err, result) {
if (err) throw err;
currentCount = result.userReviewCount;
currentCount++;
});

collections.ReviewModel.update(
{ 'filmId': review._filmId.toNumber() },
{ $set: { "userReviewCount": currentCount } },
function(err, res) {
if (err) throw err;
console.log("Result of update:" + res);
});


So this function is called whenever a user submits a review. First, it uses findOne to find my review by its Id, and fetches the current value of userReviewCount. Then, it increments this by 1, and adds it back to the collection.



But what is super weird, is that the $set thing only seems to work every other time I call the function. The first time I call it the variable userReviewCount stays at 0, the second time I call it it goes to 1, the third time it stays at 1, the fourth time it goes to 2 and so forth. I cannot get my head around why it is operating like this. To debug I outputed the res to the console, but the result is always "Result of update:[object Object]" Which gives me no information.



Does anyone have any ideas how I can debug this? I've thought of everything.










share|improve this question













marked as duplicate by Neil Lunn javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 2:41


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • Don't separate the find and update, there's a single method for that. There's also $inc which increments atomically. Not to mention use JSON.stringify to avoid the [object Object] thing.

    – Neil Lunn
    Nov 22 '18 at 2:43













  • Thanks I've updated to use the $inc!

    – mcplums
    Nov 22 '18 at 14:57
















0
















This question already has an answer here:




  • How to increment a field in mongodb?

    1 answer



  • Update And Return Document In Mongodb

    4 answers



  • what does [object Object] mean?

    7 answers




I have a movie review website. I have two collections in my mongo db, one for my movie reviews, and one for reviews submitted by users.



Whenever a user submits their own review, I want to increment the variable userReviewCount within the collection of my reviews.



I'm using mongoose and running everything within javascript:



function saveUserReview(review) {

collections.ReviewModel.findOne(
{ 'filmId': review._filmId.toNumber() },
function(err, result) {
if (err) throw err;
currentCount = result.userReviewCount;
currentCount++;
});

collections.ReviewModel.update(
{ 'filmId': review._filmId.toNumber() },
{ $set: { "userReviewCount": currentCount } },
function(err, res) {
if (err) throw err;
console.log("Result of update:" + res);
});


So this function is called whenever a user submits a review. First, it uses findOne to find my review by its Id, and fetches the current value of userReviewCount. Then, it increments this by 1, and adds it back to the collection.



But what is super weird, is that the $set thing only seems to work every other time I call the function. The first time I call it the variable userReviewCount stays at 0, the second time I call it it goes to 1, the third time it stays at 1, the fourth time it goes to 2 and so forth. I cannot get my head around why it is operating like this. To debug I outputed the res to the console, but the result is always "Result of update:[object Object]" Which gives me no information.



Does anyone have any ideas how I can debug this? I've thought of everything.










share|improve this question













marked as duplicate by Neil Lunn javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 2:41


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • Don't separate the find and update, there's a single method for that. There's also $inc which increments atomically. Not to mention use JSON.stringify to avoid the [object Object] thing.

    – Neil Lunn
    Nov 22 '18 at 2:43













  • Thanks I've updated to use the $inc!

    – mcplums
    Nov 22 '18 at 14:57














0












0








0









This question already has an answer here:




  • How to increment a field in mongodb?

    1 answer



  • Update And Return Document In Mongodb

    4 answers



  • what does [object Object] mean?

    7 answers




I have a movie review website. I have two collections in my mongo db, one for my movie reviews, and one for reviews submitted by users.



Whenever a user submits their own review, I want to increment the variable userReviewCount within the collection of my reviews.



I'm using mongoose and running everything within javascript:



function saveUserReview(review) {

collections.ReviewModel.findOne(
{ 'filmId': review._filmId.toNumber() },
function(err, result) {
if (err) throw err;
currentCount = result.userReviewCount;
currentCount++;
});

collections.ReviewModel.update(
{ 'filmId': review._filmId.toNumber() },
{ $set: { "userReviewCount": currentCount } },
function(err, res) {
if (err) throw err;
console.log("Result of update:" + res);
});


So this function is called whenever a user submits a review. First, it uses findOne to find my review by its Id, and fetches the current value of userReviewCount. Then, it increments this by 1, and adds it back to the collection.



But what is super weird, is that the $set thing only seems to work every other time I call the function. The first time I call it the variable userReviewCount stays at 0, the second time I call it it goes to 1, the third time it stays at 1, the fourth time it goes to 2 and so forth. I cannot get my head around why it is operating like this. To debug I outputed the res to the console, but the result is always "Result of update:[object Object]" Which gives me no information.



Does anyone have any ideas how I can debug this? I've thought of everything.










share|improve this question















This question already has an answer here:




  • How to increment a field in mongodb?

    1 answer



  • Update And Return Document In Mongodb

    4 answers



  • what does [object Object] mean?

    7 answers




I have a movie review website. I have two collections in my mongo db, one for my movie reviews, and one for reviews submitted by users.



Whenever a user submits their own review, I want to increment the variable userReviewCount within the collection of my reviews.



I'm using mongoose and running everything within javascript:



function saveUserReview(review) {

collections.ReviewModel.findOne(
{ 'filmId': review._filmId.toNumber() },
function(err, result) {
if (err) throw err;
currentCount = result.userReviewCount;
currentCount++;
});

collections.ReviewModel.update(
{ 'filmId': review._filmId.toNumber() },
{ $set: { "userReviewCount": currentCount } },
function(err, res) {
if (err) throw err;
console.log("Result of update:" + res);
});


So this function is called whenever a user submits a review. First, it uses findOne to find my review by its Id, and fetches the current value of userReviewCount. Then, it increments this by 1, and adds it back to the collection.



But what is super weird, is that the $set thing only seems to work every other time I call the function. The first time I call it the variable userReviewCount stays at 0, the second time I call it it goes to 1, the third time it stays at 1, the fourth time it goes to 2 and so forth. I cannot get my head around why it is operating like this. To debug I outputed the res to the console, but the result is always "Result of update:[object Object]" Which gives me no information.



Does anyone have any ideas how I can debug this? I've thought of everything.





This question already has an answer here:




  • How to increment a field in mongodb?

    1 answer



  • Update And Return Document In Mongodb

    4 answers



  • what does [object Object] mean?

    7 answers








javascript mongodb mongoose






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 22:32









mcplumsmcplums

114




114




marked as duplicate by Neil Lunn javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 2:41


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Neil Lunn javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 2:41


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • Don't separate the find and update, there's a single method for that. There's also $inc which increments atomically. Not to mention use JSON.stringify to avoid the [object Object] thing.

    – Neil Lunn
    Nov 22 '18 at 2:43













  • Thanks I've updated to use the $inc!

    – mcplums
    Nov 22 '18 at 14:57



















  • Don't separate the find and update, there's a single method for that. There's also $inc which increments atomically. Not to mention use JSON.stringify to avoid the [object Object] thing.

    – Neil Lunn
    Nov 22 '18 at 2:43













  • Thanks I've updated to use the $inc!

    – mcplums
    Nov 22 '18 at 14:57

















Don't separate the find and update, there's a single method for that. There's also $inc which increments atomically. Not to mention use JSON.stringify to avoid the [object Object] thing.

– Neil Lunn
Nov 22 '18 at 2:43







Don't separate the find and update, there's a single method for that. There's also $inc which increments atomically. Not to mention use JSON.stringify to avoid the [object Object] thing.

– Neil Lunn
Nov 22 '18 at 2:43















Thanks I've updated to use the $inc!

– mcplums
Nov 22 '18 at 14:57





Thanks I've updated to use the $inc!

– mcplums
Nov 22 '18 at 14:57












1 Answer
1






active

oldest

votes


















0














You're having a problem with async code.



You are increasing current count after your database has responded, but you send the set query before you have incremented the response



Also, you dont need to query it twice, you can get the result from the findOne, change it and save



function saveUserReview(review) {
var query = { 'filmId': review._filmId.toNumber() }

collections.ReviewModel.findOne(query, function(err, result) {
if (err) throw err;
result.userReviewCount++;
// could also be result.set(userReviewCount, result.userReviewCount + 1)
result.save();

});
}


I recommend reading about the async nature of javascript



Also, the reason you can't debug, is you are adding an object to a string so it transforms the object to a string [Object object] and prints it. The right way would be console.log("Result of update:", res); or make two different console.log






share|improve this answer


























  • Dude this is super helpful, thank you so much. Yes I have no understanding at all of the async nature of javascript. I will get learning!

    – mcplums
    Nov 22 '18 at 14:57


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You're having a problem with async code.



You are increasing current count after your database has responded, but you send the set query before you have incremented the response



Also, you dont need to query it twice, you can get the result from the findOne, change it and save



function saveUserReview(review) {
var query = { 'filmId': review._filmId.toNumber() }

collections.ReviewModel.findOne(query, function(err, result) {
if (err) throw err;
result.userReviewCount++;
// could also be result.set(userReviewCount, result.userReviewCount + 1)
result.save();

});
}


I recommend reading about the async nature of javascript



Also, the reason you can't debug, is you are adding an object to a string so it transforms the object to a string [Object object] and prints it. The right way would be console.log("Result of update:", res); or make two different console.log






share|improve this answer


























  • Dude this is super helpful, thank you so much. Yes I have no understanding at all of the async nature of javascript. I will get learning!

    – mcplums
    Nov 22 '18 at 14:57
















0














You're having a problem with async code.



You are increasing current count after your database has responded, but you send the set query before you have incremented the response



Also, you dont need to query it twice, you can get the result from the findOne, change it and save



function saveUserReview(review) {
var query = { 'filmId': review._filmId.toNumber() }

collections.ReviewModel.findOne(query, function(err, result) {
if (err) throw err;
result.userReviewCount++;
// could also be result.set(userReviewCount, result.userReviewCount + 1)
result.save();

});
}


I recommend reading about the async nature of javascript



Also, the reason you can't debug, is you are adding an object to a string so it transforms the object to a string [Object object] and prints it. The right way would be console.log("Result of update:", res); or make two different console.log






share|improve this answer


























  • Dude this is super helpful, thank you so much. Yes I have no understanding at all of the async nature of javascript. I will get learning!

    – mcplums
    Nov 22 '18 at 14:57














0












0








0







You're having a problem with async code.



You are increasing current count after your database has responded, but you send the set query before you have incremented the response



Also, you dont need to query it twice, you can get the result from the findOne, change it and save



function saveUserReview(review) {
var query = { 'filmId': review._filmId.toNumber() }

collections.ReviewModel.findOne(query, function(err, result) {
if (err) throw err;
result.userReviewCount++;
// could also be result.set(userReviewCount, result.userReviewCount + 1)
result.save();

});
}


I recommend reading about the async nature of javascript



Also, the reason you can't debug, is you are adding an object to a string so it transforms the object to a string [Object object] and prints it. The right way would be console.log("Result of update:", res); or make two different console.log






share|improve this answer















You're having a problem with async code.



You are increasing current count after your database has responded, but you send the set query before you have incremented the response



Also, you dont need to query it twice, you can get the result from the findOne, change it and save



function saveUserReview(review) {
var query = { 'filmId': review._filmId.toNumber() }

collections.ReviewModel.findOne(query, function(err, result) {
if (err) throw err;
result.userReviewCount++;
// could also be result.set(userReviewCount, result.userReviewCount + 1)
result.save();

});
}


I recommend reading about the async nature of javascript



Also, the reason you can't debug, is you are adding an object to a string so it transforms the object to a string [Object object] and prints it. The right way would be console.log("Result of update:", res); or make two different console.log







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 '18 at 15:21

























answered Nov 21 '18 at 22:43









iagowpiagowp

1,40511224




1,40511224













  • Dude this is super helpful, thank you so much. Yes I have no understanding at all of the async nature of javascript. I will get learning!

    – mcplums
    Nov 22 '18 at 14:57



















  • Dude this is super helpful, thank you so much. Yes I have no understanding at all of the async nature of javascript. I will get learning!

    – mcplums
    Nov 22 '18 at 14:57

















Dude this is super helpful, thank you so much. Yes I have no understanding at all of the async nature of javascript. I will get learning!

– mcplums
Nov 22 '18 at 14:57





Dude this is super helpful, thank you so much. Yes I have no understanding at all of the async nature of javascript. I will get learning!

– mcplums
Nov 22 '18 at 14:57





Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

Npm cannot find a required file even through it is in the searched directory