2012-03-31 54 views
48

我以这种方式回到猫鼬文档的JSON:转换猫鼬文档以JSON

UserModel.find({}, function (err, users) { 
    return res.end(JSON.stringify(users)); 
} 

然而,也返回用户.__ proto__。没有它我怎么能回来?我想这但不工作的:

UserModel.find({}, function (err, users) { 
    return res.end(users.toJSON()); // has no method 'toJSON' 
} 

回答

93

你也可以尝试mongoosejs的lean()

UserModel.find().lean().exec(function (err, users) { 
    return res.end(JSON.stringify(users)); 
} 
+0

好的,它似乎是答案。 – 2013-01-04 14:50:27

+5

不应该是:'JSON。stringify(users);'因为用'lean()'返回的文档是纯JS对象? – enyo 2013-01-09 14:30:23

+0

是的,你是对的,谢谢。应该使用JSON.stringify(用户)。 – ecdeveloper 2013-10-20 11:13:17

21

首先尝试toObject()代替toJSON()可能?

其次,你需要调用它的实际文件,而不是阵列,所以也许尝试一些更恼人的是这样的:

var flatUsers = users.map(function() { 
    return user.toObject(); 
}) 
return res.end(JSON.stringify(flatUsers)); 

这是一个猜测,但我希望它能帮助

+0

有映射它是如此讨厌,是不是有什么东西在图书馆这样做呢? – Antoine 2017-09-14 03:49:59

5

我发现我犯了一个错误。根本不需要调用toObject()或toJSON()。问题中的__proto__来自jquery,而不是猫鼬。下面是我的测试:

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)中没有区别。在这种情况下不需要。

37

晚的答案,但在定义架构时你也可以试试这个。

/** 
* toJSON implementation 
*/ 
schema.options.toJSON = { 
    transform: function(doc, ret, options) { 
     ret.id = ret._id; 
     delete ret._id; 
     delete ret.__v; 
     return ret; 
    } 
}; 

注意ret是JSON'ed对象,这不是猫鼬模型的实例。你将在对象散列上对它进行操作,而不使用getter/setter。

然后:

Model 
    .findById(modelId) 
    .exec(function (dbErr, modelDoc){ 
     if(dbErr) return handleErr(dbErr); 

     return res.send(modelDoc.toJSON(), 200); 
    }); 

编辑:2015年2月

因为我没有提供一个解决缺少的toJSON(或toObject)方法(S)我会解释的区别我的使用示例和OP的使用示例。

OP:

UserModel 
    .find({}) // will get all users 
    .exec(function(err, users) { 
     // supposing that we don't have an error 
     // and we had users in our collection, 
     // the users variable here is an array 
     // of mongoose instances; 

     // wrong usage (from OP's example) 
     // return res.end(users.toJSON()); // has no method toJSON 

     // correct usage 
     // to apply the toJSON transformation on instances, you have to 
     // iterate through the users array 

     var transformedUsers = users.map(function(user) { 
      return user.toJSON(); 
     }); 

     // finish the request 
     res.end(transformedUsers); 
    }); 

我的例子:

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()); 
     } 
    }); 
+3

这是最好的方式去。 – 2014-12-05 20:08:43

+0

toJSON()未定义 – OMGPOP 2015-02-17 08:13:19

+0

@eAbi toJSON和toObject都未定义 – OMGPOP 2015-02-17 08:13:49

11
model.find({Branch:branch},function (err, docs){ 
    if (err) res.send(err) 

    res.send(JSON.parse(JSON.stringify(docs))) 
}); 
+0

这感觉hacky,但工程! – sidonaldson 2015-11-10 16:10:40

+0

这是此问题的最佳答案。隐藏猫鼬技术领域的'魔术'似乎隐藏在JSON.stringify的后面。 – mischka 2017-03-27 14:08:53

0

您可以使用res.json()来jsonify任何对象。 ()将删除猫鼬查询中的所有空字段。

UserModel.find().lean().exec(function (err, users) { return res.json(users); }

0

试试这个选项:

UserModel.find({}, function (err, users) { 
    return res.end(JSON.parse(JSON.stringify(users))); 
    //Or: 
    //return JSON.parse(JSON.stringify(users)); 
    }