• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

218
Views
cómo configurar el valor predeterminado de la variable dentro del objeto en el esquema Mongoose

tengo este esquema:

 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)

y usando esta mutación para agregar un elemento:

 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 },

Sin embargo, al agregar un artículo, la moneda permanece nula, ¿qué me estoy perdiendo aquí? Puedo agregar la moneda en la mutación addItem:

 itemPrice.currency = 'USD'

sin embargo, debe haber una mejor manera, ¿no es así?

Gracias por tu contribución.

about 3 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error