'The promise rejected with the reason "[object Array]".] { code: 'ERR_UNHANDLED_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(). The promise rejected with the reason "[object Array]".] {
code: 'ERR_UNHANDLED_REJECTION'
}
const uuid = require("uuid").v4
const path = require("path")
const fs = require("fs")
class Prescriptions{
constructor(title){
this.title = title
this.id = uuid()
}
toJson(){
return{
title: this.title,
id: this.id
}
}
async save(){
const prescription = await Prescriptions.getAll()
prescription.push(this.toJson())
return new Promise((reject, resolve)=>{
fs.writeFile(
path.join(__dirname, "..", "data", "prescriptions.json"),
JSON.stringify(prescription),
(err) => {
if(err){
reject(err)
} else{
resolve(err)
}
}
)
})
}
static getAll(){
return new Promise((reject, resolve) =>{
fs.readFile(
path.join(__dirname, '..', 'data', 'prescriptions.json'),
'utf-8',
(err, content) =>{
if(err){
reject(err)
} else{
resolve(JSON.parse(content))
}
}
)
})
}
}
module.exports = Prescriptions
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
