Reputation: 51
I am trying to save a new document to a collection, but rather than taking the parameters from the Model() constructor or Model.create() method, an empty object is created. I am probably doing something wrong or missing a small detail somewhere but I am currently stuck. My mongoDB database is hosted locally on mongodb for windows.
I have a schema and model:
import mongoose from 'mongoose';
const CardSchema = new mongoose.Schema({
sideA: String,
sideB: String,
})
export const CardSetSchema = new mongoose.Schema({
user_email: String,
name: String,
cards: [CardSchema],
});
const CardSet = mongoose.model('CardSet', CardSchema);
export default CardSet
I have an endpoint trying to make a new document:
.post(async (req, res) => {
const obj = { user_email: req.user_email, name: req.body.name, cards: [] };
const cardSet = new CardSet(obj);
await cardSet.save();
res.status(201).json(cardSet);
})
When looking at the data with console.log the object and cardSet look the following:
{ user_email: '[email protected]', name: 'wa', cards: [] }
{ _id: new ObjectId("62481f4964d4b1789c3110c3") }
My connection URL looks like this:
mongodb://localhost:27017/flash-card-test
When I check MongoDB Compass the collection is indeed being populated with empty objects.
Does anyone have any idea what could be going wrong here? Many thanks!
Upvotes: 0
Views: 1635
Reputation: 51
It was a mistake. I built a model from the CardSchema rather than the CardSet schema.
Upvotes: 1