Node.js fs.writeFile: 错误 returns 空
Node.js fs.writeFile: err returns null
我正在使用 'fs' 写入文件。写入过程很顺利,文件如我所愿创建,但 'err' 变量 returns 为空。我认为这个 'null' 表示没有错误,但我想确定一下。
'null' from err in fs.writeLine 函数是否意味着没有错误?
Is having 'null' from err in fs.writeFile function means there are no errors?
是的。
nodejs中几乎所有异步回调所使用的nodejs异步调用约定是将两个参数传递给回调。第一个是 err
值,如果它是 null
(或任何虚假值),则没有错误,异步结果在第二个参数中(如果有结果值) .
如果err
不为假,则代表错误值。
这通常被称为 nodejs 异步调用约定,并在大量 nodejs 函数中使用。
这是一篇 explains/confirms 这篇参考文章:What are the error conventions?。
因为fs.writeFile()
只有一个error/success条件,没有任何其他结果,所以fs.writeFile()
通常的使用方式是这样的:
fs.writeFile('someFile', someData, function(err) {
if (err) {
// there was an error
console.log(err);
} else {
// data written successfully
console.log("file written successfully");
}
});
我正在使用 'fs' 写入文件。写入过程很顺利,文件如我所愿创建,但 'err' 变量 returns 为空。我认为这个 'null' 表示没有错误,但我想确定一下。
'null' from err in fs.writeLine 函数是否意味着没有错误?
Is having 'null' from err in fs.writeFile function means there are no errors?
是的。
nodejs中几乎所有异步回调所使用的nodejs异步调用约定是将两个参数传递给回调。第一个是 err
值,如果它是 null
(或任何虚假值),则没有错误,异步结果在第二个参数中(如果有结果值) .
如果err
不为假,则代表错误值。
这通常被称为 nodejs 异步调用约定,并在大量 nodejs 函数中使用。
这是一篇 explains/confirms 这篇参考文章:What are the error conventions?。
因为fs.writeFile()
只有一个error/success条件,没有任何其他结果,所以fs.writeFile()
通常的使用方式是这样的:
fs.writeFile('someFile', someData, function(err) {
if (err) {
// there was an error
console.log(err);
} else {
// data written successfully
console.log("file written successfully");
}
});