0

If I add console.log(req.body.biddingGroup) to my PUT method then it returns

[ [ { bidderId: '5dd5b31213372b165872bf5b' } ] ]

Therefore I know that the value is being passes properly, but if I add the line biddingGroup: req.body.biddingGroup

the bid never updates to mongodb. If I remove that one line, then the bid updates.

Not sure why it's not being passed to mongodb. I also tried in monogoose model

biddingGroup: [{
    type: String
}]

but that returned the error

Cast to string failed for value "{}" at path "biddingGroup"

mongoose schema


const mongoose = require('mongoose');


const postSchema
 = mongoose.Schema({
title: {type: String},
startingBid: {type: String},
bidderId: {type: String},
currentBid: {type: String},
lastBidTimeStamp: {type: Date},
increments: {type: String},
shippingCost: {type: String},
auctionType: {type: String},
buyItNow: {type: String},
snipingRules: {type: String},
auctionEndDateTime: {type: String},
biddingGroup: {bidderId: {type: String}},
currentBid: { type: String, require: true },
lastBidTimeStamp: { type: Date, required: true },
creator: { type: mongoose.Schema.Types.ObjectId, ref: "User"},
 });


const Auction = mongoose.model('Listing', postSchema);


module.exports = Auction;

app.js

app.put('/api/listings/:id', (req, res) => {
  console.log(req.body.biddingGroup);
  Post.findByIdAndUpdate({ _id: req.params.id },
    {
      currentBid: req.body.currentBid,
      lastBidTimeStamp: Date.now(),
      bidderId: req.body.bidderId,
      biddingGroup: req.body.biddingGroup,
      auctionEndDateTime: req.body.auctionEndDateTime
    }, function (err, docs) {
      if (err) res.json(err);
      else {
        console.log(docs)
      }
    });
});

I appreciate any help!

1
  • 1
    Can you share your Post model? Make sure it has the same structure as the body you're sending to it Commented Nov 24, 2019 at 21:25

1 Answer 1

1

From your mongoose schema you are specifying the "biddingGroup" to be an array of strings, but what you actually get (based on your console output) is an array of arrays of objects, each of those objects then should have a property called bidderId, which is a string.

To achieve a schema that matches what you get in your console.log, you can do the following:

const postSchema = new mongoose.Schema({
    ...
    biddingGroup: [[{bidderId: String}]],
    ...});
Sign up to request clarification or add additional context in comments.

2 Comments

I'm a little bit confused by what you mean. I updated the code above to reflect what I think you're asking, but I'm still having the same problem so I probably did it wrong.
Can you try the following in the postschema: biddingGroup: [[{bidderId: String}]],

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.