2012-03-31 56 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)); 
    }