Node A method in an object called another method of that object

the code is as follows

const File = require("../lib/file").File
const path = require("path")
const fs = require("fs")

let handleFile = {
    // 
    create (file) {
        return File.create(file)
    },
    //  id 
    getFileById (id) {
        return File
            .findOne({ _id: id })
            // .addCreatedAt()
            .exec()
    },
}
//  id 
handleFile.deleteFileById = function async (id) {
    let fileObject = await handleFile.getFileById(id)
    fs.unlink(path.join("../uploads",fileObject.filename))
    return File
        .remove({ _id: id })
        .exec()
}
// 
// fileOperate.getFilePath = function async (id) {
//     let fileObject = await file_operate.getFileById(id)
//     return path.join("../uploads",fileObject.filename)
// }
module.exports = handleFile

screenshots of the error report are as follows

clipboard.png

nodejs version 8.9.3 async/await can be used normally

Mar.04,2021

async is there a mistake?


async should be placed in front of function.


//  id 
handleFile.deleteFileById = async function (id) {
    let fileObject = await handleFile.getFileById(id)
    fs.unlink(path.join('../uploads',fileObject.filename))
    return File
        .remove({ _id: id })
        .exec()
}
Menu