简体   繁体   English

为什么回调函数的第一个参数是 null?

[英]Why callback function has null as first argument?

I read everything what I could find,but still I do not understand this.我读了所有我能找到的东西,但我仍然不明白这一点。

Passportjs authentication(Twitter) authorize Passportjs 认证(推特)授权

function(token, tokenSecret, profile, done) {
    Account.findOne({ domain: 'twitter.com', uid: profile.id }, function(err, account) {
      if (err) { return done(err); }
      if (account) { return done(null, account); }

Why is the err callback only with oneargument,and the second account callback has also null argument?为什么 err 回调只有一个参数,而第二个帐户回调也有 null 参数?

done in this code is also a callback function which conforms to a convention called error first这段代码中done也是一个回调函数,它符合一个叫做error first的约定

There's really only two rules for defining an error-first callback:定义错误优先回调实际上只有两条规则:

  • The first argument of the callback is reserved for an error object.回调的第一个参数是为错误对象保留的。 If an error occurred, it will be returned by the first err argument.如果发生错误,它将由第一个 err 参数返回。
  • The second argument of the callback is reserved for any successful response data.回调的第二个参数是为任何成功的响应数据保留的。 If no error occurred, err will be set to null and any successful data will be returned in the second argument.如果没有发生错误,err 将被设置为 null,任何成功的数据都将在第二个参数中返回。

Therefore when an error occurs done is called with a single parameter - the error.因此,当发生错误时done使用单个参数调用 - 错误。 When no error occurs done is called with 2 parameters.当没有错误发生时done用 2 个参数调用。 The first is null (no error) the second is the sucessful response details.第一个是空(没有错误),第二个是成功的响应细节。

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

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