Mongoose trying to use set function to fields that are missing
See original GitHub issueVersions: Node.js: 14.17.5 Mongoose: 5.13.7 MongoDB: 4.4.8
Problem: Mongoose trying to use set function to fields that are missing.
Schema:
const {Schema, Document} = require('mongoose')
const {dateNowWithoutMs, clearStringFromSpaces} = require('../utils/setData')
const toInt = value => {
return value ^ 0
}
const eventSchema = new Schema({
title: {type: String, minlength: 3, maxlength: 100, set: clearStringFromSpaces},
startDate: {type: Number, min: dateNowWithoutMs(), max: dateNowWithoutMs() * 2, set: toInt},
endDate: {type: Number, min: dateNowWithoutMs(), max: dateNowWithoutMs() * 2, set: toInt},
_apperDate: {type: Number, default: dateNowWithoutMs(), select: false},
__v: {type: Number, select: false}
})
eventSchema.statics ={
create: async function({ title = 'Default' , startDate, endDate }) {
let event = new this({
title,
startDate,
endDate,
_apperDate: dateNowWithoutMs()
})
console.log('Event presaved:', event) //Result will be presented below
event = await event.save()
return event
}
}
eventSchema.set(
'toJSON',
{
getters: true,
transform: function(doc, ret) {
delete ret._id
}
}
)
module.exports = eventSchema
Example code:
const db = require('../db')
const test = async() => {
const data = {
title: 'test'
}
const event = await db.Event.create(data)
}
test()
Console.:
Event presaved: {
title: "test",
_id: 613fa66056fd93333499f711,
_apperDate: 1631561312
}
Error text:
Event validation failed: startDate: Cast to Number failed for value \"NaN\" (type number) at path \"startDate\", endDate: Cast to Number failed for value \"NaN\" (type number) at path \"endDate\"
Issue Analytics
- State:
- Created 2 years ago
- Comments:7
Top Results From Across the Web
Nodejs Mongoose still return fields if missing - Stack Overflow
In this case you can add default in your schema. If you do this, an empty string is saved by default for that...
Read more >Setting and getting fields not defined in schema not working ...
What is the expected behavior? I was expecting to remove the fields set as undefined from the database. I did put the full...
Read more >$exists — MongoDB Manual
To query for null or missing fields, see Query for Null or Missing Fields. Use a Sparse Index to Improve $exists Performance. The...
Read more >Express Tutorial Part 3: Using a Database (with Mongoose)
The very first thing we want to do is set up a MongoDB database that we can use to store our library data....
Read more >Mongoose v6.8.1: Defaults
Your schemas can define default values for certain paths. If you create a new document without that path set, the default will kick...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This is expected behavior because you’re explicitly setting
startDate
andendDate
toundefined
. When you donew this({ startDate, endDate })
, that means you’re always settingstartDate
andendDate
. I’d recommend you do this instead:Here is the complete test code: