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

250
Views
array destructuring mongoose create

is there any other way to insert a data into mongoose without using array desctructuring i have some code below, it doesnt work, also it doesnt insert correctly into database

const data = req.file.originalname.split('.')[0].split('_');
if (data.length < 5) throw new Error('Invalid file name');

const content = await fs.readFile(req.file.path, 'utf8');
await orders.create({ data, content });

i can make this work by using this code by using array desctructuring like below, what i want to know is there any way without using desctructuring, and just using variable data like my code above

const data = req.file.originalname.split('.')[0].split('_');
if (data.length < 5) throw new Error('Invalid file name');

// const [no_telp, type, timespan, name, unique_code] = data;
const content = await fs.readFile(req.file.path, 'utf8');
await orders.create({ no_telp, type, timespan, name, unique code, content });
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

What you are doing is not array destructuring. Array destructuring means pulling data out of array. An example array destructuring could be const listCopy = [...list] or const listAdded = [...list, 12, 48]. If you mean this part create({ no_telp, type, timespan, name, unique code, content }); you are providing neessary data into create method. You can create an abject beforehand and just send pass it to create method. const userData = { no_telp, type, timespan, name, unique code, content }; await orders.create(userData); Additionally, what you are trying to save is a stringified data. After reading a file with fs.readFile() you must parse it to manipulate and save in database correctly. Try this:

const stringData = await fs.readFile(req.file.path, 'utf8');
const content = JSON.parse(stringData)
console.log(content) // see the data
const userData = {no_telp, type, timespan, name, unique code, content};
await orders.create(userData);
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!