Sunday 23 December 2018

Mongoose findOneAndUpdate can't take an object?

hello all,​I'm working on learning how to build API's and I'm having some trouble with my PUT. With my POST I can do something like:​const userToAdd = new User({firstName: req.body.firstName,lastName: req.body.lastName,password: hashed,email: req.body.email.toLowerCase(),address: req.body.address,city: req.body.city,state: req.body.state,zip: req.body.zip});let result = await userToAdd.save();​The above works just fine, however when I try and do a findOneAndUpdate, I have to break out each field instead of just creating a new User object.​const user = await User.findOneAndUpdate({_id: id},{firstName : \${firstName}`,`\lastName : `${lastName}`,`\password : `${hashed}`,`\email : \${email}`,`\address : \${address}`,`\city : \${city}`,`\state : \${state}`,`\zip : \${zip}`},`{ new: true })\`​I expected I would be able to do something likeconst userToUpdate = {firstName: req.body.firstName,lastName: req.body.lastName,password: hashed,email: req.body.email.toLowerCase(),address: req.body.address,city: req.body.city,state: req.body.state,zip: req.body.zip};const user = await User.findOneAndUpdate({_id: id}, userToUpdate, {new: true})​kind regards

Submitted December 24, 2018 at 02:55AM by sma92878

No comments:

Post a Comment