如何从外部函数更新对象?

挣扎着这种mongoosefunction。 我正在尝试在另一个embedded对象中添加一个embedded对象,但是它将新的出价对象添加到错误的地方 – 为我的迭代器创build的新列表variables。 我for循环试图find确切的清单更新,所以我认为分配他们的行动是搞砸了他们。 例如,如果列表是users[2].listings[10].bids ,那么我怎样才能到达那个对象,以便更新它呢?

  function create(req, res) { db.Listing.findById(req.params.listingId, function(err, foundListing) { // console.log(foundListing._id ); if (err) { console.log('Error', err); } // res.json(foundListing); // get array of all users db.User.find({}, function(err, users) { // for loop iterates through all users' listings for (let i = 0; i < users.length; i++) { let listings = users[i].listings // for loop iterates through all listings ids for (let j = 0; j < listings.length; j++) { // finds match // comparing _id with _id returning false. Not sure why, will return later if (listings[j].topic === foundListing.topic && listings[j].created === foundListing.created) { console.log("Found match: " + foundListing.topic); // get current user id to add to bid object db.User.findById(req.user, function(err, user) { if (err) { console.log(err); } var newBid = new db.Bid(req.body); // add data validation later newBid.uid = user._id // pushes new bid object into embedded listing listings[j].bids.push(newBid); listings[j].save(function(err, savedBid) { console.log('newBid created: ', newBid); console.log(listings[j]); res.json(newBid); }); }); } } } }) if (err) { console.log(err); } }); }; 

编辑 – 有这么远,但现在它似乎不像我的arrays正在保存。

 function create(req, res) { db.Listing.findById(req.params.listingId, function(err, foundListing) { if (err) { console.log('Error:', err); } db.User.findOne({ 'listings._id': req.params.listingId }, function(err, foundUser) { // console.log(foundUser.listings); if (err) { console.log('Error: ', err); } for (let i = 0; i < foundUser.listings.length; i++) { // console.log(foundUser.listings[i]._id); if (foundUser.listings[i]._id == req.params.listingId) { console.log( 'found it! - ' + foundUser.listings[i].topic); var newBid = new db.Bid(req.body); // console.log(newBid) foundUser.listings[i].bids.push(newBid); console.log(foundUser.listings[i].bids) foundUser.listings[i].save(function(err, savedListing) { // console.log(foundUser.listings[i]) if (err) { console.log('Error: ', err); return; } console.log('newBid created: ', newBid); console.log('savedListing', savedListing); res.json(newBid); }) } } }) }); }; 

你的代码很难理解。 我想你可能会使用错误的工具来解决问题。 MongoDb和Mongoose似乎有相当先进的查询,所以你可以指定什么确切的用户,列表等你感兴趣的。

我认为这将是值得您花时间来看看Mongoose查询和也许MongoDb查询的文档。 的. 用于db.User.findOne({ 'listings.id': req.params.listingId }中的db.User.findOne({ 'listings.id': req.params.listingId } 在这里描述 。

我不能真正testing这个代码编译或者它会工作,因为我不知道你的数据库模型等等,但是应该是这样:

  function create(req, res) { db.Listing.findById(req.params.listingId, function(err, foundListing) { // use the power of mongo db and mongoose. // you can specify more exactly what you're looking for // in this case we're interested in ONE user that // has a listing with a specific id // (if you still problems with comparing identical IDs, then that's a serious issue.. // maybe try logging req.params.listingId and check if it really exists // in your database.) db.User.findOne({ 'listings.id': req.params.listingId }, function(err, foundUser) { var newBid = new db.Bid(req.body); // add data validation later newBid.uid = foundUser._id // Old code: foundListing.bids.push(newBid) // Change according to first question/answer at http://mongoosejs.com/docs/faq.html // which was linked in the thread you linked to in comment. // The essence is that Mongoose doesn't get // notified about changes to arrays by default // To get around this you can update using "set" // but then you need both a value and an index: var oldBidsLength = foundListing.bids.length; foundListing.bids.set(oldBidsLength, newBid); foundListing.save(function(err, savedBid) { // savedBid = saved LISTING? console.log('newBid created: ', newBid); console.log('savedBid', savedBid); res.json(savedBid); } } }) }); }; 

这个代码示例可能closures的事情是,如果db.User.findOne({ 'listings.id': req.params.listingId }应该与db.User.findOne({ 'listings.id': req.params.listingId }一起使用,我不太了解您的模型。