最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What is the null argument to cb in multer? - Stack Overflow

programmeradmin4浏览0评论

In the code below, from the multer API, the two cb functions take null as their first argument. What is the significance of null and what other values might be used here other than null?

var storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, '/tmp/my-uploads')
  },
  filename: function (req, file, cb) {
    cb(null, file.fieldname + '-' + Date.now())
  }
})

var upload = multer({ storage: storage }

In the code below, from the multer API, the two cb functions take null as their first argument. What is the significance of null and what other values might be used here other than null?

var storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, '/tmp/my-uploads')
  },
  filename: function (req, file, cb) {
    cb(null, file.fieldname + '-' + Date.now())
  }
})

var upload = multer({ storage: storage }
Share Improve this question asked Apr 30, 2019 at 21:15 user3425506user3425506 1,4474 gold badges19 silver badges28 bronze badges 1
  • 1 Normally when using callbacks it's a mon practice to make the first argument of a callback an error state. eg. callback(new Error("oops something went wrong")); – Keith Commented Apr 30, 2019 at 21:18
Add a ment  | 

3 Answers 3

Reset to default 7

Asynchronous functions which take a callback often format the callback such that the first argument provided to the callback is the error, if any error is encountered, while the second argument is the successful retrieved value (if no error is encountered). That's exactly what's happening here. If destination or filename involved something which might throw an error, then the first argument you pass to cb could be the error, for example:

destination: function (req, file, cb) {
  if (!authorized) {
    cb(new Error('You are not authorized to do this!'));
    return;
  }
  cb(null, '/tmp/my-uploads')
}

The reasoning is, if the first argument is the error, the module that passes cb is incentivized to use and examine the first argument, allowing for proper error handling.

If the error was passed as the second argument, for example, it would be easy for lazy programmers to simply ignore it, and define the callback such that it only looks at the first argument.

This is the error-first callback pattern established in the early days of Node.JS core and libraries developed in its ecosystem. It is still a mon pattern, but mostly subsumed by things like promises or async/await. Here is the relevant section from the Node.JS docs https://nodejs/api/errors.html#errors_error_first_callbacks.

The other option other than null would be an instance of some type of Error.

The null means there was no error and you are calling the callback with a successful pletion and a resulting value.

The node.js asynchronous callback convention is for a callback that takes two parameters that would look like this if it was a declared function:

function someCallbackFunction(err, value) {
    if (err) {
        // process err here
    } else {
        // no error, process value here
    }
}

The first parameter is an error (null if no error, usually an instance of Error object if there is an error). The second is a value (if there was no error).

So, you pass null as the first argument when there is no error and the second argument will contain your value.

FYI, there is node.js documentation for this callback style.

发布评论

评论列表(0)

  1. 暂无评论