简体   繁体   中英

How to fix code: 'ERR_INVALID_CALLBACK' nodejs

I'm very new to nodejs. I've been following a udemy instructor and after copying and understanding what he demonstrated, my compiler gives me an 'ERR_INVALID_CALLBACK' error. Please help me understand. Thank you!

const fs = require('fs');

if(!fs.exists("views")){

    fs.mkdir("views", (err)=>{

        if(err) return err;

        fs.writeFile("./views/new.html", 'this is a new dir and data', (err)=>{

            if (err) return err;

            console.log('Directory and File saved!')
        })
    })
}

Just like fs.mkdir and fs.writeFile , the methodfs.exists is async and expects a callback. If you want to "wait" for it to finish, use fs.existsSync() :

const fs = require('fs');

if(!fs.existsSync("views")){

    fs.mkdir("views", (err)=>{

        if(err) return err;

        fs.writeFile("./views/new.html", 'this is a new dir and data', (err)=>{

            if (err) return err;

            console.log('Directory and File saved!')
        })
    })
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM