简体   繁体   English

TypeError [ERR_INVALID_CALLBACK]中理解中的问题:回调必须是节点中的函数

[英]issue in understanding in `TypeError [ERR_INVALID_CALLBACK]: Callback must be a function` in node

i am new to node and having issue in understanding why this error TypeError [ERR_INVALID_CALLBACK]: Callback must be a function is occurs by running the command node file-create-directory.js 我是Node的新手,并且在理解为什么出现此错误TypeError [ERR_INVALID_CALLBACK]: Callback must be a function遇到问题TypeError [ERR_INVALID_CALLBACK]: Callback must be a function通过运行命令node file-create-directory.js发生TypeError [ERR_INVALID_CALLBACK]: Callback must be a function

file-create-directory.js file is: file-create-directory.js文件是:

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 directory and data', (err) =>{
       if(err) return err;
        console.log('Directory and file saved');
    });
  });    
}

First of all fs.exists is deprecated 首先不推荐使用fs.exists

const fs = require('fs');
fs.access('views', fs.constants.F_OK, (err) => {
  // if it does not exists
  if(err) {
     fs.mkdir('views', (err) => {
       if(!err) { 
         fs.writeFile('./views/new.html', 'this is a new directory and data', 
         (err) => {
           if(err) return err;
           console.log('Directory and file saved');
         }
     });
  }
});    

But if you decide to use fs.exists use this 但是,如果您决定使用fs.exists使用此选项

fs.exists('views', (exists) => {
  if(!exists) {
     fs.mkdir('views',(err) => {
       if(err) return err;
       fs.writeFile('./views/new.html','this is a new directory and 
       data', (err) => {
         if(err) return err;
         console.log('Directory and file saved');
       });
     });  
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError [ERR_INVALID_CALLBACK]: 回调必须是一个函数 - TypeError [ERR_INVALID_CALLBACK]: Callback must be a function TypeError [ERR_INVALID_CALLBACK]:回调必须是Express JS函数 - TypeError [ERR_INVALID_CALLBACK]: Callback must be a function Express JS 如何修复此错误 TypeError [ERR_INVALID_CALLBACK]: Callback must be a function - how to fix this error TypeError [ERR_INVALID_CALLBACK]: Callback must be a function TypeError [ERR_INVALID_CALLBACK]:回调必须是 function。 在nodejs中收到未定义 - TypeError [ERR_INVALID_CALLBACK]: Callback must be a function. Received undefined in nodejs 我收到TypeError [ERR_INVALID_CALLBACK]:回调必须是一个函数 - I have got TypeError [ERR_INVALID_CALLBACK]: Callback must be a function TypeError [ERR_INVALID_CALLBACK]: 回调必须是 function。接收未定义 - TypeError [ERR_INVALID_CALLBACK]: Callback must be a function. Received undefined Maven ERR_INVALID_CALLBACK( - Maven ERR_INVALID_CALLBACK( express --view=hbs myapp getting [ERR_INVALID_CALLBACK]: Callback must be a function - express --view=hbs myapp getting [ERR_INVALID_CALLBACK]: Callback must be a function [ERR_INVALID_CALLBACK]:使用 fs.writeFile 时,回调必须是 function - [ERR_INVALID_CALLBACK]: Callback must be a function when using fs.writeFile 抛出新的 ERR_INVALID_CALLBACK(回调); - throw new ERR_INVALID_CALLBACK(callback);
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM