将Mongoose文档转换为json
我以json的形式返回mongoose文档:将Mongoose文档转换为json,json,node.js,object,document,mongoose,Json,Node.js,Object,Document,Mongoose,我以json的形式返回mongoose文档: UserModel.find({}, function (err, users) { return res.end(JSON.stringify(users)); } 但是,还返回了user.\uuuu proto\uuuu。没有它我怎么能回来?我试过了,但没有成功: UserModel.find({}, function (err, users) { return res.end(users.toJSON()); // ha
UserModel.find({}, function (err, users) {
return res.end(JSON.stringify(users));
}
但是,还返回了user.\uuuu proto\uuuu。没有它我怎么能回来?我试过了,但没有成功:
UserModel.find({}, function (err, users) {
return res.end(users.toJSON()); // has no method 'toJSON'
}
首先,尝试
toObject()
而不是toJSON()
其次,您需要在实际文档而不是数组上调用它,因此可能需要尝试以下更烦人的方法:
var flatUsers = users.map(function() {
return user.toObject();
})
return res.end(JSON.stringify(flatUsers));
这只是猜测,但我希望能有所帮助。我发现我犯了一个错误。根本不需要调用toObject()或toJSON()。问题中的原型来自jquery,而不是mongoose。这是我的测试:
UserModel.find({}, function (err, users) {
console.log(users.save); // { [Function] numAsyncPres: 0 }
var json = JSON.stringify(users);
users = users.map(function (user) {
return user.toObject();
}
console.log(user.save); // undefined
console.log(json == JSON.stringify(users)); // true
}
doc.toObject()从文档中删除doc.prototype。但是在JSON.stringify(doc)中没有区别。在这种情况下不需要它。您也可以尝试mongoosejs的:
回答晚了,但您也可以在定义模式时尝试这一点
/**
* toJSON implementation
*/
schema.options.toJSON = {
transform: function(doc, ret, options) {
ret.id = ret._id;
delete ret._id;
delete ret.__v;
return ret;
}
};
请注意,ret
是JSON对象,它不是mongoose模型的实例。您将直接在对象哈希上操作它,而不使用getter/setter
然后:
Model
.findById(modelId)
.exec(function (dbErr, modelDoc){
if(dbErr) return handleErr(dbErr);
return res.send(modelDoc.toJSON(), 200);
});
编辑:2015年2月
因为我没有为缺少的toJSON(或toObject)方法提供解决方案,所以我将解释我的使用示例和OP的使用示例之间的差异
作品:
我的例子是:
UserModel
.findById(someId) // will get a single user
.exec(function(err, user) {
// handle the error, if any
if(err) return handleError(err);
if(null !== user) {
// user might be null if no user matched
// the given id (someId)
// the toJSON method is available here,
// since the user variable here is a
// mongoose model instance
return res.end(user.toJSON());
}
});
可以使用res.json()对任何对象进行jsonify。
lean()将删除mongoose查询中的所有空字段
UserModel.find().lean().exec(函数(err,用户){
返回res.json(用户);
}
尝试以下选项:
UserModel.find({}, function (err, users) {
//i got into errors using so i changed to res.send()
return res.send( JSON.parse(JSON.stringify(users)) );
//Or
//return JSON.parse(JSON.stringify(users));
}
答案可能有点误会,但如果有人想用另一种方法,您可以使用
Model.hydrate()
(自mongoose v4以来)将javascript对象(JSON)转换为mongoose文档
使用Model.aggregate(…)
是一个很有用的例子。因为它实际上返回的是纯JS对象,所以您可能希望将其转换为mongoose文档,以便访问Model.method
(例如,在架构中定义的虚拟财产)
另外,我认为它应该有一个运行类似“将json转换为Mongoose文档”的线程,但实际上没有,因为我已经找到了答案,所以我认为做自我发布和自我回答是不好的。这对我来说很有效:
Products.find({})。然后(a=>console.log(a.map(p=>p.toJSON()))
另外,如果要使用getter,还应添加其选项(在定义架构时):
newmongoose.Schema({…},{toJSON:{getters:true}})
不应该是:JSON.stringify(用户);
因为返回的文档是纯JS对象吗?是的,你说得对,谢谢.JSON.stringify(用户)应使用。如果查询数据库后仍要在回调函数中使用mongoose实例对象,则不应使用lean
函数。请参阅我的答案以了解解决方案。:)注意,lean()
将删除虚拟属性这是最好的方法。@eAbi toJSON和toObject都不是defined@OMGPOPtoJSON和toObject都是在mongoose模型实例上定义的方法。您可以提供您的使用示例,也可以在stackoverflow上发布另一个问题。据我所知,无论使用何种Mongoose版本,toJSON和toObject方法都没有被弃用/删除。@eAbi它不在那里。询问者也有同样的问题。您确定要调用toJSON而不是JSON.stringify()?@OMGPOP是的,我确定我使用的是toJSON方法。OP的用法示例和我的不同之处在于,在OP的问题中,返回的users
变量是一个mongoose实例数组。您必须遍历数组,并在每个实例上调用toJSON方法。在我的示例中,我使用findById方法,它直接将找到的mongoose实例传递给回调函数。然后您可以直接在这个实例上调用toJSON(或toObject)方法。这是这个问题的最佳答案。隐藏猫鼬技术领域的“魔力”似乎隐藏在JSON.stringify的后面。必须映射它是如此烦人,库中不是有什么东西可以做到这一点吗?
UserModel
.findById(someId) // will get a single user
.exec(function(err, user) {
// handle the error, if any
if(err) return handleError(err);
if(null !== user) {
// user might be null if no user matched
// the given id (someId)
// the toJSON method is available here,
// since the user variable here is a
// mongoose model instance
return res.end(user.toJSON());
}
});
model.find({Branch:branch},function (err, docs){
if (err) res.send(err)
res.send(JSON.parse(JSON.stringify(docs)))
});
UserModel.find({}, function (err, users) {
//i got into errors using so i changed to res.send()
return res.send( JSON.parse(JSON.stringify(users)) );
//Or
//return JSON.parse(JSON.stringify(users));
}