Mongoose返回字符串而不是对象

时间:2017-07-19 18:20:10

标签: node.js mongodb mongoose

我在尝试使用mongoose查询mongodb时遇到了一些小问题。 以下是我的代码示例:

...
resolve: (root, params) => {
  var user = User.findOne({email: params.data.email}).exec()
  return user;
}
...

返回以下内容:

{
  "data": {
"login": "{ _id: 596f4cc4f51fa12bf0f5a001,\n  updatedAt: 2017-07-19T12:12:52.736Z,\n  createdAt: 2017-07-19T12:12:52.736Z,\n  email: 'myemail@gmail.com',\n  password: '$2a$12$bhPG4TPGR6by/UBTeAnzq.lyxhfMAJnBymDbkFDIHWl5.XF2JG62O',\n  __v: 0 }"

} }

我不知道为什么会这样。任何帮助将不胜感激。

修改
这是完整的代码:

var bcrypt = require('bcryptjs');
var _ = require('lodash');
var { GraphQLNonNull, GraphQLString } = require('graphql');
var jwt = require('jsonwebtoken');

var { UserInputType } = require('./UserSchema');
var User = require('./UserModel');

var login = {
  type: new GraphQLNonNull(GraphQLString),
  args: {
    data: {
        name: 'Data',
        type: new GraphQLNonNull(UserInputType)
    }
  },
  resolve: (root, params, { SECRET }) => {
    var user = User.findOne({email: params.data.email}).exec();
    var authorized = bcrypt.compareSync(params.data.password, user.password);
    if (!authorized) throw new Error('Invalid password');
    var token = jwt.sign({
        user: _.pick(user, ['_id', 'name'])
    }, SECRET, {expiresIn: '1y'});
    return token;
  }
};

2 个答案:

答案 0 :(得分:1)

这可能在调用解析和调用的函数中。然后是此用户承诺。没有更多代码就无法确定......

答案 1 :(得分:0)

Get-Content file | ForEach-Object {
  if ($_ -match '\bDynamic \d+/50') {
    $_ # output line as is
  } elseif (($fields = -split $_) -and $fields[2] -match '^Server') {
    $fields[1, 2, 4, 5] -join ' ' # output fields of interest, joined with spaces
  }
}

决定这样做而瞧!解决了我的问题。谢谢你的帮助。