'how to set-up default value of variable inside of object in Mongoose schema
I have this schema:
const mongoose = require('mongoose')
const schema = new mongoose.Schema({
itemDate: {
type: String,
required: true,
},
itemName: {
type: String,
required: true,
},
itemPrice: {
type: Object,
required: true,
price: {
type: String,
required: true,
},
currency: {
type: String,
default: 'USD',
},
},
})
module.exports = mongoose.model('Item', schema)
and using this mutation for adding item:
Mutation: {
addItem: async (_, args) => {
const { itemName, itemPrice } = args.itemInput
let { itemDate } = args
if (!itemDate) {
itemDate = new Date()
}
const item = new Item({ itemDate, itemName, itemPrice })
await item.save()
return item
},
However when adding item, the currency remains null, what am I missing here? I can add the currency in the addItem mutation:
itemPrice.currency = 'USD'
however there must be a better way, isn't that so?
Thanks for your input.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
