【问题标题】:Loopback crash in callback when calling on resetPasswordRequest more then one调用 resetPasswordRequest 时,回调中的环回崩溃不止一个
【发布时间】:2016-08-05 15:37:05
【问题描述】:

我正在尝试使用自己的远程方法来重置密码。用户只需将电子邮件放在路径中,服务器将发送 accessToken 作为响应。

MyUser.remoteMethod('requestPasswordReset',
        {
          http: {path: '/reset/:usermail', verb: 'post'},
          accepts: [{arg: 'usermail', type: 'string', required: true, http: {source: 'path'}}],
          returns: {root: true, type: 'object'},
          description: 'Allows Users to request a password reset, returns an error if the specified email does not exist'
        }
);

MyUser.requestPasswordReset = function (usermail, next) {
    MyUser.resetPassword({email: usermail}, function (err) {
      if (err) {
        next(err);
      }
      else {
        MyUser.on('resetPasswordRequest', function (info) {

          console.log('next will be executed, but crashes the second time');
          next(null, info.accessToken.id); 
        });
      }
    })
  };

我第一次向/reset/:usermail 发出请求时一切正常,但第二次运行请求时应用程序崩溃并出现以下错误

发送后无法设置标头

我知道调用更多回调时通常会发生此错误。 如果我给回调一个其他错误参数。例如next('abc') 而不是 null next(null, info.accessToken.id); 应用程序不会崩溃

【问题讨论】:

    标签: node.js callback loopbackjs strongloop


    【解决方案1】:

    我找到了错误的原因。 MyUser.on('resetPasswordRequest', function (info)) 正在做一个听众。所以每次调用requestPasswordReset 时,都会创建一个新的监听器。当用户第二次发出请求时,有两个监听器,它们都发送响应。把MyUser.on('resetPasswordRequest', function (info))放在js文件的根目录下就没问题了。

    【讨论】:

    • 你说“把.放在 js 的根目录下”是什么意思?
    • @TimTuckle 我的意思是把函数移到文件的根目录
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-16
    相关资源
    最近更新 更多