I am trying to insert a document to MongoDB using Mongoose in ES6. I have the following code to create the document:
Passes.create(
{
passDate: '20147',
invitedGuestPass: [{
residentId: new mongoose.Types.ObjectId(),
residentFirstName: 'Jack',
residentLastName: 'Smith',
residentEmail: '[email protected]',
invitedGuestId: new mongoose.Types.ObjectId(),
invitedGuestName: request.body.guestName,
invitedGuestNumber: request.body.guestNumber,
invitedGuestPassScanned: false,
invitedGuestPassSent: false
}]
}
)
.then((result) => {
console.log(result);
})
.catch((error) => {
console.log(error);
})
And the Schema is the following:
const mongoose = require("mongoose");
const invitedGuestPassesSchema = new mongoose.Schema({
residentId: {
type: mongoose.Schema.ObjectId,
required: [true, "Please provide and ID..."],
unique: false
},
residentFirstName: {
type: String,
required: [true, "Please provide the resident's first name..."],
unique: false
},
residentLastName: {
type: String,
required: [true, "Please provide the resident's last name..."],
unique: false
},
residentEmail: {
type: String,
required: [true, "Please provide the resident's email..."],
unique: false
},
invitedGuestId: {
type: mongoose.Schema.ObjectId,
required: [true, "Please provide and ID..."],
unique: false
},
invitedGuestName: {
type: String,
required: [true, "Please provide the guest's name..."],
unique: false
},
invitedGuestNumber: {
type: String,
required: [true, "Please provide the guest's phone number..."],
unique: false
},
invitedGuestPassScanned: {
type: Boolean,
required: [true, "Please provide if the pass has been scanned..."],
unique: false
},
invitedGuestPassSent: {
type: Boolean,
required: [true, "Please provide if the pass has been sent..."],
unique: false
},
})
const passesSchema = new mongoose.Schema({
passDate: {
type: String,
required: [true, "Please give date of visit..."],
unique: true
},
invitedGuestPass: {
invitedGuestPass: [invitedGuestPassesSchema],
type: Array,
required: [true, "Please give list of invited guests..."],
unique: false
},
})
module.exports = mongoose.model.Passes || mongoose.model("Passes", passesSchema);
When I run the script, the document is successfully created, but the "invitedGuestPass" array is set to undefined instead of the object that I passed in.
{
passDate: '20147',
invitedGuestPass: [ undefined ],
_id: new ObjectId("65bda2eb1dd48467790b137e"),
__v: 0
}
I’m using "mongoose": "^7.6.3",
Why is the array undefined?
I’ve tried simplifying the schema to be just an array of integers, and passed in [1,2,3], but that gave the same undefined array.
I’ve set "type" to [invitedGuestPassesSchema] in passesSchema -> invitedGuestPass and deleted "invitedGuestPass: [invitedGuestPassesSchema]". This still left it undefined.
When I set it to string and pass a string, it successfully creates the document with the correct data. So everything is correct. It’s just when I pass an array that it doesn’t work.
I checked that js reads the input as an array using Array.isArray(…) and it returned true.
I’ve tried using insert and insertOne, but it gives an error "insert is not a function…"
2
Answers
change this piece of code to be like this :
you’re trying to create a document of
invitedGuestPassesSchema
right insidepassesSchema
while the document itself hasn’t been created yet so how can Mongoose know about data that hasn’t been stored yet? you have to create the document ofinvitedGuestPassesSchema
first then add its_id
topassesSchema
.using this approach the latter will hold documents of the other model.
I’ve marked
unique: false
as a comment becausefalse
is the default value so no need to define it unless you want it to betrue
.You have a nested
invitedGuestPass
inside yourinvitedGuestPass
property. Mongoose will default to an empty array if it cannot evaluate one from your create method execution. Change yourpassesSchema
like so: