@Vlad242

Где ошибка в коде?

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(). (rejection id: 4)
const uuid = require('uuid/v4')
const fs = require("fs")
const path = require("path")


class Course {
    constructor(title,price,img) {
        this.title = title
        this.price = price
        this.img = img
        this.id = uuid()
    }

    toJSON() {
        return {
            title:this.title,
            price:this.price,
            img:this.img,
            id:this.id
        }
    }

    async save() {
        const courses = await Course.getAll()
        courses.push(this.toJSON)


        return new Promise(( resolve, reject) => {
            fs.writeFile(
                path.join(__dirname, '..', 'data', 'courses.json'),
                JSON.stringify(courses),
                (err) => {
                    if (err) {
                        reject(err)
                    } else {
                        resolve()
                    }
                }
            )
            })
    }


    static getAll() {
        return new PromiseRejectionEvent((resolve, reject) => {
            fs.readFile(
                path.join(__dirname, "..", "data", "courses.json"),
                'utf-8',
                (err, content) => {
                    if (err) {
                        reject (err)
                    } else {
                        resolve(JSON.parse(content))
                    }
    
                }
    
            )
        })
       
    }
}
module.exports = Course
  • Вопрос задан
  • 1214 просмотров
Решения вопроса 1
joeberetta
@joeberetta Куратор тега JavaScript
Читай: https://epdf.pub/google-for-dummies.html
Согласно новому стандарту теперь будет вываливаться, если промисы/подобные не будут отлавливаться. Вам сейчас ошибка говорит, что вы .catch() (т.е. ошибки) не обработали
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы