Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js NodeJS:Hapi+;Mongoose |回调返回不工作_Node.js_Mongoose_Callback_Promise_Hapijs - Fatal编程技术网

Node.js NodeJS:Hapi+;Mongoose |回调返回不工作

Node.js NodeJS:Hapi+;Mongoose |回调返回不工作,node.js,mongoose,callback,promise,hapijs,Node.js,Mongoose,Callback,Promise,Hapijs,我正在使用hapi和mongoose来实现一个简单的登录系统 server.route({ method: 'POST', path: '/register', config: { handler: (request, h) => { const username = request.payload.username.toLowerCase(); const email = request.payload.email.toLowerCase(); const p

我正在使用hapi和mongoose来实现一个简单的登录系统

server.route({
method: 'POST',
path: '/register',
config: {
  handler: (request, h) => {
    const username = request.payload.username.toLowerCase();
    const email = request.payload.email.toLowerCase();
    const password = request.payload.password;
    const passwordconfirm = request.payload.passwordconfirm;
    if(password==passwordconfirm && username && email && password) {
      var userData = {
        email: email,
        username: username,
        password: password,
        verified: false
      }
      User.create(userData, function (err, user) {
        //if there is an error about email
        if (err.toString().indexOf("email")>-1) {
          return statusGenerator(false, "Email already in use", null);
        }

        //if there is an error about email
        if (err.toString().indexOf("username")>-1) {
          return statusGenerator(false, "Username already in use", null);
        }

        //send email to specified email
        return statusGenerator(true,"Confirm your email",null);
      });
     }
   }
 }
});
statusGenerator()生成一个JSON,该JSON应作为响应发送回。 在hapi中,我可以从处理程序返回以实现这一点,但我不知道如何从回调中获取该值并作为响应发送。 这里是错误

Debug: internal, implementation, error
Error: handler method did not return a value, a promise, or throw an error
at module.exports.internals.Manager.execute (c:\Users\me\path\node_modules\hapi\lib\toolkit.js:52:29)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
调试:内部、实现、错误
错误:处理程序方法未返回值、承诺或引发错误
在module.exports.internal.Manager.execute(c:\Users\me\path\node\u modules\hapi\lib\toolkit.js:52:29)
在
在进程中。_tick回调(内部/process/next_tick.js:188:7)

我用承诺解决了这个问题:

     return new Promise((resolve) => {
        User.create(userData, function (err, user) {
          if(err)
          {
            if (err.toString().indexOf("email")>-1) {
              resolve(statusGenerator(false, "Email already in use", null));
            }

            if (err.toString().indexOf("username")>-1) {
              resolve(statusGenerator(false, "Username already in use", null));
            }
          }
          //send email to specified email
          resolve(statusGenerator(true,"Confirm your email",null));
        });
      });