简体   繁体   中英

Can't add array to mongodb

I'm trying to send an array to mongodb, but the res.json(user) returns an empty biddingGroup:[] and mongodb document never has field biddingGroup appear. I've looked at stack posts and have seen suggestions for schema. I've tried

biddingGroup: [{type: String}],
biddingGroup: [String],
biddingGroup: {type: String},

I haven't found a working schema that captures the data yet.

I even hardcoded biddingGroup: ['test'] too, but it never shows up.

app.js

app.put('/api/listings/:id', (req, res) =>

Post.update({
  id: req.query.id
}, {
  $set: {
    currentBid: req.query.currentBid,
    lastBidTimeStamp: req.params.lastBidTimeStamp,
    biddingGroup: ['test']
  }
}, {
  multi: false //set to false to ensure only one document gets updated
}).exec().then(data => {
  console.log(data);
}, err => {
  console.log(err);
})
    );

Any help is appreciated.

You need to use exec() at the end to run the query. That is the function that actually runs the request and returns you the promise. Plus your usage of the update function in general is off.

Try this:

Post.update({
  id: req.query.id
}, {
  $set: {
    currentBid: req.query.currentBid,
    lastBidTimeStamp: req.params.lastBidTimeStamp,
    biddingGroup: ['test']
  }
}, {
  multi: false //set to false to ensure only one document gets updated
}).exec().then(data => {
  console.log(data);
}, err => {
  console.log(err);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM