Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

279
Vistas
Retrieving data from user input and save it to mongoose model

This is my model shema:

const DemoSchema = new Schema({
    rowOne: {
        colOne: {
            one: {
                name: String,
                qty: Number,
            },
            two: {
                name: String,
                qty: Number,
            },
            three: {
                name: String,
                qty: Number,
            },
        },
    },
    rowTwo: {
        colOne: {
            one: {
                name: String,
                qty: Number,
            },
            two: {
                name: String,
                qty: Number,
            },
            three: {
                name: String,
                qty: Number,
            },
        },
    },
});

When I do this it saves data to my model:

const product = await new Regal({
    rowOne: { colOne: { two: { name: productName, qty: productQty } } },
});
product.save();

My question is, how can I replace rowOne, colOne and two with user input?

I tried this:

const row  = req.body.rows // this must be rowOne or rowTwo
const column = req.body.column // colOne or other (colOne to colNine)
const col = req.body.col // one, two or three
const productName = req.body.name
const productQty = req.body.qty

Attempt 1:

const product = await new Regal({ `${row}`: { `${column}`: { `${col}`: { name: productName, qty: productQty }}}});

'Error --> Property Assignment expected.'

Atempt 2:

const product = await new Regal(`{${row}: { ${column}: { ${col}: { name: ${productName}, qty: ${productQty} } } } }`); 

'Error --> Parameter "obj" to Document() must be an object, got {rowOne: { colOne: { one: { name: Milk, količina: 250 } } } }'

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can use variables as keys of objects using square brackets.

let user = 'username';
let newObj = { [user]:'saloni' }

this will create an object like {username:saloni}. This way you can replace rowOne, colOne, and two with user input by storing them in a variable and then use it.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda