Having an trouble with $push or Arrays...

48 views
Skip to first unread message

Travis

unread,
Jun 11, 2014, 4:35:10 AM6/11/14
to mongoo...@googlegroups.com

Hello I'm trying to wrap my head around $push specifically consuming the data that I've pushed. Any help would be appreciated

After I've pushed the array and I try to consume it's data it isn't behaving like an array. I cannot reference indexes (reviews[0]) nor can I reference it's length (reviews.length). I've tried many variations on the schema and none have fixed this issue.

The only work around that I've managed to find is to use JSON.stringify and then use JSON.parse. JSON.parse alone was throwing this error : 

[ { reviewId: '53980547f96c90151e16e048',
   ^
SyntaxError: Unexpected token r
   at Object.parse (native)


This is the Array in my db

"reviews": [
        {
           "reviewId": "53980f88bf07579a1eb50441",
           "vendorId": "537ecbb85b55cfc8904afded"
       },
       {
           "reviewId": "53980f89bf07579a1eb50442",
           "vendorId": "537ecbb85b55cfc8904afded"
       },
   ],


This is what the console logs if I simply log the array: 

[ { reviewId: '53980547f96c90151e16e048',
   userId: '5397fe7627e53dea1def348c' },
 { reviewId: '5398062332c299301e9e092f',
   userId: '5397fe7627e53dea1def348c' },]

Here is the code that generates the above array: 

var userSchema = mongoose.Schema ({
    reviews : {
       vendorId : String,
       reviewId : String
   }
   //..
});
//
updateReviewArrayUser = {$push: {reviews : {reviewId: rev._id, vendorId :vendorId }}};
User.update ({_id: userId}, updateReviewArrayUser)
                   .exec (function (err, updateReviewArrayResults)
                      {
                       console.log ('err : ', err);
                       console.log ('user results : ', updateReviewArrayResults);
                      });





Joe Wagner

unread,
Jun 11, 2014, 11:30:05 AM6/11/14
to mongoo...@googlegroups.com
He Travis,

I'm not sure if I understand your question completely.  What are you expecting the `updateReviewArrayResults` variable to be?
I believe that Model.update does not return the affected document.  In this type of situation I generally use findOneAndUpdate, or I do a second query for the changed document after the update.

Sorry if thats not helpful,
-Joe

Travis

unread,
Jun 11, 2014, 1:58:09 PM6/11/14
to mongoo...@googlegroups.com
Hi Joe, 

I understand that udpateReviewArrayResults just returns a 1. I leave the log in there to verify that it's being written to the DB. 

When I query the db (after the fact) and attempt to read the review property of the document I am running into the issue above where the property needs to go through JSON.stringify and then JSON.parse hope this is a bit more clear.

User.find ({'_id': currVendor}) 
                .exec (
                function (err, results)
                {
                     console.log(results.reviews)
// [ { reviewId: '53980547f96c90151e16e048' userId: '5397fe7627e53dea1def348c' },...]
                     console.log(results.reviews.length) //undefined
                     console.log(results.reviews[0]) //undefined
                     var string =  JSON.stringify(results.reviews);
                     var p = JSON.parse(string);
                     console.log(p.length) //10 (correct)
                }




Joe Wagner

unread,
Jun 11, 2014, 9:04:41 PM6/11/14
to mongoo...@googlegroups.com
I'm not sure whats going on.  I know that Model.find passes null, or an error object, and an array of documents to the callback.  From looking at your schema, reviews.length should be `undefined`, because an Object in javascript doesn't have a length.
I would think you should be able to do something like
console.log(results[0].reviews);// {vendorId: '123', reviewId: '722'}

But it doesn't make any sense that `results.reviews` would even exist, so I'm not sure what is happening
Can you post a working gist that demonstrates the issue?

Ryan Wheale

unread,
Jun 12, 2014, 4:06:53 AM6/12/14
to mongoo...@googlegroups.com
Your "reviews" schema is not defined properly.  You need to specify it as an array (take note of the brackets):

var userSchema = mongoose.Schema ({
    reviews : [{
       vendorId : String,
       reviewId : String
   }]
   //..
});

--
Documentation - http://mongoosejs.com/
Plugins - http://plugins.mongoosejs.com/
Bug Reports - http://github.com/learnboost/mongoose
Production Examples - http://mongoosejs.tumblr.com/
StackOverflow - http://stackoverflow.com/questions/tagged/mongoose
Google Groups - https://groups.google.com/forum/?fromgroups#!forum/mongoose-orm
Twitter - https://twitter.com/mongoosejs
IRC - #mongoosejs
---
You received this message because you are subscribed to the Google Groups "Mongoose Node.JS ODM" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongoose-orm...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages