Node.js uncaughtException-GraphQLError:请求无效

Node.js uncaughtException-GraphQLError:请求无效,node.js,graphql,express-graphql,Node.js,Graphql,Express Graphql,我试图做的是在用户登录失败时使用GraphQLError添加自定义消息 以下是app.js文件中的GraphQL端点代码: app.use('/graphql', graphqlExpress(req => ({ schema, context: { user: req.user }, formatError: function (error) { console.log(error) return error } })),function (e

我试图做的是在用户登录失败时使用GraphQLError添加自定义消息

以下是app.js文件中的GraphQL端点代码:

app.use('/graphql', graphqlExpress(req => ({
  schema,
  context: {
    user: req.user
  },
  formatError: function (error) {
    console.log(error)
    return error
  }
})),function (err, req, res, next) {
  if (err.name === 'UnauthorizedError') { // Send the error rather than to show it on the console
    //console.log(err);
      res.status(401).send(err);
  }
  else {
      next(err);
  }
}
);
以下是变异逻辑代码:

if (validator.isEmpty(input.input.username)) {
                    errors.push({ key: 'username', message: 'Username must not be empty.' });
                }
                if (validator.isEmpty(input.input.password)) {
                    errors.push({ key: 'password', message: 'Password must not be empty.' });
                }
                if (errors.length) throw new ValidationError(errors);

                UserModel.find({login:input.input.username,sts:true}).exec((err,result)=>{
                    if(err){throw new ValidationError(err);}

                    if(!result.length)
                    {
                        errors.push({ key: 'Auth', message: 'User doesn not exists.' });
                        //if (errors.length) throw new ValidationError(errors);
                        callback(null,errors);

                    }

                });
以下是ValidationError.js文件代码:

const { GraphQLError } = require('graphql');

module.exports  = class ValidationError extends GraphQLError {
  constructor(errors) {

    super('The request is invalid');

    var err = errors.reduce((result, error) => {

        if (Object.prototype.hasOwnProperty.call(result, error.key)) {
          result[error.key].push(error.message);
        } else {
          result[error.key] = [error.message];
        }

        return result;
      }, {});
        //this.message=err;
    //this.data = [{test:1}];
  }
}
我正在尝试执行以下GraphQL变异查询:

mutation{
  login(input:{username:"test",password:"1234"}) {
    _id,
    name{
      fname,
      lname,
      mname
    }
  }
}
它在GraphQL窗口中为我提供输出

{
  "data": {
    "login": null
  }
}
终端中出现未捕获异常错误:

[ { key: 'Auth', message: 'User doesn not exists.' } ]
Sun, 05 Aug 2018 08:27:57 GMT uncaughtException: The request is invalid
GraphQLError: The request is invalid
    at new ValidationError (/Volumes/Drive B/dev/zapi/graphql/ValidationError.js:6:5)
    at UserModel.find.exec (/Volumes/Drive B/dev/zapi/graphql/mutations/user/login.js:56:50)
    at /Volumes/Drive B/dev/zapi/node_modules/mongoose/lib/model.js:4454:16
    at (anonymous function).call (/Volumes/Drive B/dev/zapi/node_modules/mongoose/lib/query.js:3395:7)
    at cb (/Volumes/Drive B/dev/zapi/node_modules/mongoose/lib/query.js:1434:14)
    at result (/Volumes/Drive B/dev/zapi/node_modules/mongodb/lib/utils.js:414:17)
    at executeCallback (/Volumes/Drive B/dev/zapi/node_modules/mongodb/lib/utils.js:406:9)
    at handleCallback (/Volumes/Drive B/dev/zapi/node_modules/mongodb/lib/utils.js:128:55)
    at cursor.close (/Volumes/Drive B/dev/zapi/node_modules/mongodb/lib/operations/cursor_ops.js:218:62)
    at handleCallback (/Volumes/Drive B/dev/zapi/node_modules/mongodb/lib/utils.js:128:55)
    at completeClose (/Volumes/Drive B/dev/zapi/node_modules/mongodb/lib/cursor.js:887:14)
    at Cursor.close (/Volumes/Drive B/dev/zapi/node_modules/mongodb/lib/cursor.js:906:10)
    at cursor._next (/Volumes/Drive B/dev/zapi/node_modules/mongodb/lib/operations/cursor_ops.js:218:23)
    at handleCallback (/Volumes/Drive B/dev/zapi/node_modules/mongodb-core/lib/cursor.js:202:5)
    at _setCursorNotifiedImpl (/Volumes/Drive B/dev/zapi/node_modules/mongodb-core/lib/cursor.js:560:38)
    at self._endSession (/Volumes/Drive B/dev/zapi/node_modules/mongodb-core/lib/cursor.js:568:46)
    at Cursor._endSession (/Volumes/Drive B/dev/zapi/node_modules/mongodb-core/lib/cursor.js:193:5)
    at Cursor._endSession (/Volumes/Drive B/dev/zapi/node_modules/mongodb/lib/cursor.js:226:59)
    at _setCursorNotifiedImpl (/Volumes/Drive B/dev/zapi/node_modules/mongodb-core/lib/cursor.js:568:17)
    at setCursorNotified (/Volumes/Drive B/dev/zapi/node_modules/mongodb-core/lib/cursor.js:560:3)
    at /Volumes/Drive B/dev/zapi/node_modules/mongodb-core/lib/cursor.js:687:16
    at queryCallback (/Volumes/Drive B/dev/zapi/node_modules/mongodb-core/lib/cursor.js:267:16)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! zapi@1.0.0 start: `DEBUG=my-zapi,express* node ./bin/www`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the zapi@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /var/root/.npm/_logs/2018-08-05T08_27_57_178Z-debug.log
为什么它会给我未捕获的异常错误?请帮忙


提前感谢。

您是否检查了“用户不存在”的位置。错误来自哪里(请注意,它有输入错误,可能在您的代码库中)?它看起来像来自(graphql/mutations/user/login.js:56:50),mongo就是找不到具有这样字段值的用户。我已经更新了变异逻辑代码。我忘了把那封信寄出去。我在用户登录失败时添加了此“用户不存在”消息。请再次查看邮件。