Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

246
Views
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.

about 4 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 Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!