Trying to add two records into mongoDB: one as a new collection and the other as a reference ID.
Here is a sample schema: Company{ name: Acme Company, Locations: [refID] }
The ID and location payload will be sent in the request body.
addNewLocation: (req, res) => {
let {id, ...payload} = req.body;
db.Location.create(payload)
.then( record => {
let refId = record._id
db.Company.findOneAndUpdate( {_id: id} , { $push: { locations: refId } }, { new: true })
})
.then( result => {
res.status(201).json({success: true},{result})
})
.catch( err => {
res.status(422).json({success: false},{err})
})
}
When viewing the update on Robo3T, it seems that a document is being inserted into the location but not the ref ID in the companies collection. Any thoughts on what might be causing this?