【发布时间】:2019-05-11 21:27:31
【问题描述】:
我在一个项目中同时使用mongoose 和express-graphql。我想使用 here 描述的 .toObject() 方法将文档转换为对象,然后将值返回给 GraphQL。
GraphQL 架构:
type User {
_id: ID,
// ...
}
代码:
const Me = function (params, req) {
if (!req.user) throw Error('Not logged in');
return req.user.toObject();
};
module.exports = Me;
但是,我收到以下错误:
ID 不能代表值:{ _bsontype: "ObjectID", id:
}
我尝试使用console.log(req.user.toObject()._id) 将_id 打印到屏幕上,但它只是显示了id 字符串。出于某种原因,以下代码有效:
const Me = function (params, req) {
if (!req.user) throw Error('Not logged in');
return JSON.parse(JSON.stringify(req.user.toObject()));
};
module.exports = Me;
我不确定是什么导致 GraphQL 直接从 .toObject() 方法将 _id 字段作为非字符串值读取。谢谢您的帮助!
【问题讨论】:
-
graphql 版本是什么?
-
@ShivamPandey GraphQL 14.0.2