2015-12-22 17 views
0

首先,我很抱歉我的英語。在nodejs和mongo中接收到一個空的[] json

我有和的NodeJS蒙戈的問題,我創建了這個代碼的模式:

var mongoose = require('mongoose'), 
    Schema = mongoose.Schema; 

var forumSchema = new Schema({ 
     foro:[{ 
       universidad:{ type: Schema.ObjectId, ref: "College"}, 
       topics:[{ 
         usercreator:{type:Schema.ObjectId, ref:"User"}, 
         titulo:{ type: String }, 
         posts:[{ 
           mensaje:{ type: String }, 
           username:{type:Schema.ObjectId, ref: "User"} 
          }] 
       }] 
     }] 
}); 

module.exports = mongoose.model('Forum', forumSchema); 

和我創建了一個JSON和我已經導入它MONGO:

{ 
    "_id": {"$oid":"567895dd239410651a8e4df8"}, 
    "foro":[ 
    { 
     "universidad":"566b279b747cfb38bc2830df", 
     "topics":[ 
     { 
      "usercreator": "566df8c2c7d87f1a47fda4e7", 
      "titulo": "parking cercano", 
      "posts": [ 
      { 
       "mensaje": "hay parking cerca de la uni?", 
       "username": "566df8c2c7d87f1a47fda4e7" 
      }, 
      { 
       "mensaje": "si, tiene uno grande justo al lado", 
       "username": "566df8c2c7d87f1a47fda4e5" 
      } 
      ] 
     }, 
     { 
      "usercreator":"566df8c2c7d87f1a47fda4e5", 
      "titulo":"horario", 
      "posts":[ 
      { 
       "mensaje":"solo hay horario de mañana?", 
       "username":"566df8c2c7d87f1a47fda4e5" 
      }, 
      { 
       "mensaje":"no, también de tardes", 
       "username":"566df8c2c7d87f1a47fda4e7" 
      } 
      ] 
     } 
     ] 
    } 
    ] 
} 

問題是,當我試圖在POSTMAN中獲取它時,我收到一個空的json,如:[]。

控制器的代碼是:

var mongoose = require('mongoose'); 
var Forum = mongoose.model('Forum'); 
var College = mongoose.model('College'); 
var User = mongoose.model('User'); 

//GET - Return all forum 
exports.AllForum = function(req, res) { 
    console.log('all forum'); 
    Forum.find(function(err, forum) { 
     User.populate(forum, {path:"User", select:"username"},function(err, forum){}); 
     College.populate(forum, {path:"College", select:"college"},function(err, forum) {}); 
     if (err) res.send(500, err.message); 
     console.log('GET /forum' + JSON.stringify(forum)); 
     res.status(200).jsonp(forum); 
    }); 
}; 

控制檯返回日誌「的所有論壇」,所以正在執行的代碼,但我不知道爲什麼不起作用。

有誰知道嗎?謝謝!!

PD:我可以在mongo的命令行中看到完整的json。

+0

這行是什麼打印console.log('GET/forum'+ JSON.stringify(forum)); ? – Dnyanesh

+0

在控制檯中查看json包含的內容。它顯示了郵遞員的一樣。 –

回答

0

jsonp當你向郵遞員索取郵件時,這個郵件會有一個回調。

res.status(200).json(forum);

+0

嗨,謝謝你的回答。它不工作,我也試過.send(論壇),但不起作用 –

0

取代它嘗試傳遞一個空的查詢對象到您的Forum.find操作。它看起來像你試圖填充每個返回的論壇文件中的一些領域,所以嘗試像這樣鏈接.populate

exports.AllForum = function(req, res) { 
    console.log('all forum'); 
    Forum.find({}) 
     .populate('foro.universidad', 'field1 field2') 
     .populate('foro.topics.usercreator', 'field1 field2') 
     .populate(...) 
     .exec(function(err, forum) { 
      if (err) {return res.status(500).send(err.message);} 
      console.log('GET /forum' + JSON.stringify(forum)); 
      res.status(200).jsonp(forum); 
     }); 
}; 
+0

你好,謝謝你的答案!我試過這個,但不起作用。事實上,如果我不使用填充,郵差也顯示我一個空的json。 –