my code is not working and i get the following error:
TypeError: inventors.push is not a function
at file:///C:/Users/tomma/OneDrive/Bureaublad/taller%20de%20programacion%202/javascriptFiles/TP2-2C-c/async/ejercicioPromesas.js:10:19
(node:9536) UnhandledPromiseRejectionWarning: ReferenceError: inventors is not defined
at file:///C:/Users/tomma/OneDrive/Bureaublad/taller%20de%20programacion%202/javascriptFiles/TP2-2C-c/async/ejercicioPromesas.js:15:46
(Use `node --trace-warnings ...` to show where the warning was created)
(node:9536) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection,
use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:9536) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
code:
import fs from 'fs/promises'
const path = './inventors.json';
const inventor = {first: 'Rene', last: ' Favarolo', year: 1923};
fs.readFile(path, 'utf-8').then(data =>{
const inventors = JSON.parse(data);
console.log(inventors);
await inventors.push(inventor);
return fs.writeFile (path,JSON.stringify(inventors, null, ' '))
}).then( () => (
console.log('Archivo actualizado')
)
);
Please help me
Please use the Try Catch statement.
import fs from 'fs/promises' const path = './inventors.json';
const inventor = {first: 'Rene', last: ' Favarolo', year: 1923};
fs.readFile(path, 'utf-8').then(async(data) =>{
try{
const inventors = JSON.parse(data);
console.log(inventors);
await inventors.push(inventor);
}
catch(err){
}
}
return fs.writeFile (path,JSON.stringify(inventors, null, ' '))
}).then( () => ( console.log('Archivo actualizado') ) );
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.