Mongoose函数显示变量为undefined?

时间:2017-09-07 17:00:37

标签: javascript node.js mongoose

盯着这一段时间并不确定为什么它不起作用。我试图在嵌入对象中创建一个对象,但是,似乎我没有正确地传递params,因为它表示listing [j]未定义。我需要将哪些内容传递给该片段才能发挥作用?

function createBid(req, res) {
  //get current listing object info
  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 (var i = 0; i < users.length; i++) {
        var listings = users[i].listings
        // for loop iterates through all listings ids
        for (var 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);
                res.json(newBid);
              });
            });
          }
        }
      }
    })
    if (err) {
      console.log(err);
    }
  });
};

0 个答案:

没有答案