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

278
Views
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 answers
Answer question

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 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!