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

159
Views
Los valores predeterminados no se agregan al modelo de mangosta

Así que este es un ejemplo de mi esquema que tengo para un usuario.

 id: String, email: String, slug: { type: Object, phrase: {type: String, default: null}, },

Cuando quiero definir un nuevo usuario y guardar ese usuario, haría lo siguiente;

 const newUser = new User({ id: 123, username: "CoolUser", email: "BillGates@google.com" }); newUser.save();

Pero esto no guarda el objeto "slug". Entendí que dado que tengo un valor predeterminado para él, se completaría automáticamente con ese valor predeterminado. ¿Qué puedo hacer para que se genere automáticamente sin tener que volver a definir todo el esquema al guardar un usuario?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Debe agregar el valor predeterminado para la propiedad slug , y no para su subpropiedad. Intenta cambiar tu esquema de esta manera:

 slug: { type: { phrase: { type: String }, }, default: { phrase: null }, },
about 4 years ago · Juan Pablo Isaza Report

0

prueba esto:

 const subschema = new Schema({ phrase: {type: String, default: null}, }, { _id: false });

y en su esquema original:

 id: String, email: String, slug: { type: subschema, default: () => ({}) },

Esto debería funcionar.

about 4 years ago · Juan Pablo Isaza Report

0

const User = new Schema({ id: String, about: { bio: String, location: String, website: String, discord: String, twitter: String, default: { bio: "This is a default bio.", location: "", website: "", discord: "", twitter: "", } } })

Ok, aquí está el ejemplo, eliminé algunas cosas solo para facilitarlo. Básicamente, como puede ver, quiero que la biografía tenga el valor predeterminado: This is a default bio.

Pero en su lugar, me sale el siguiente error:

 throw new TypeError(`Invalid schema configuration: \`${name}\` is not ` + ^ TypeError: Invalid schema configuration: `This is a default bio.` is not a valid type at path `about.default.bio`. See bit ly / mongoose-schematypes for a list of valid schema types.
about 4 years ago · Juan Pablo Isaza Report
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!