2013-03-05 115 views
3

,我有一個架構和子文檔的架構是這樣,所以我有這個問題,讓我忙過去的4天數:貓鼬返回JSON列表

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

var ProjectSchema = new Schema({ 
    name: String, 
    author: String, 
    category: String, 
    description: String, 
    tags: [TagSchema] 
}); 

var TagSchema = new Schema({ 
    name: String, 
    date: Date 
}); 

mongoose.model('TagSchema', TagSchema); 
mongoose.model('Project', Project); 

,我想什麼要有一個所有ProjectSchemas的所有標籤的列表,無論我嘗試我要麼NONE或只是最當前的項目。我只是不知道進一步,因爲無論我做什麼,我總是最終失敗。我做錯了什麼,是否沒有找到貓鼬所有的東西?

app.get('/tags.json', function(req, res) { 
    TagSchema.find({}, function (err, tags){ 
     var json = JSON.stringify(tags); 
     console.log(json); 
    tags = tags.map(function (tag) { 
     return tag.toObject(); 
    }); 
    console.log(json == JSON.stringify(tags)); 
    res.json(json); 
    }); 
}); 

app.get('/tags.json', function(req, res) { 
    Project.find(function (err, projects) { 
     projects.forEach(function(project){ 
      res.json(project.tags); 
     }); 
    }); 
}); 

和其他任何我想剛回到

[ ] 

或出錯了...

(另外我不知道,我怎麼能確保如果我添加一個標籤到一個項目,並且它已經存在,我可以如何保持它不被添加。)

回答

3

您試圖在模式上調用find,當您嘗試在模型上調用它時。

如果你改變你的文件的底部:

var TagModel  = mongoose.model('TagModel', TagSchema); 
var ProjectModel = mongoose.model('ProjectModel', Project); 

,然後在app.get函數調用:

app.get('/tags.json', function(req, res) { 
    TagModel.find({}, function (err, tags){ //changed this to the model instead of the schema 
     var json = JSON.stringify(tags); 
     console.log(json); 
    tags = tags.map(function (tag) { 
     return tag.toObject(); 
    }); 
    console.log(json == JSON.stringify(tags)); 
    res.json(json); 
    }); 
}); 

app.get('/tags.json', function(req, res) { 
    ProjectModel.find(function (err, projects) { 
     projects.forEach(function(project){ 
      res.json(project.tags); 
     }); 
    }); 
}); 

模型的構造從編譯您的模式定義並表示可以保存和查詢的文檔分貝。

+0

具有確定的一切都像你說幫了我一下,作爲第二碼終於顯示了JSON的標籤,但我發現它只返回最後編輯的項目中的標籤而不是所有存在的標籤。 實際上,如果我添加 console.log(JSON.stringify(project.tags)); 它顯示在控制檯中的所有人。 – 2013-03-05 16:41:00

+0

加入 'var taglist = [];'在那裏 和 'taglist.push(project.tags);' into foreach的技巧如果我在json中呈現taglist數組! – 2013-03-05 16:52:09

2

當你在自己的模型中使用TagSchema並嵌入ProjectSchema像你這樣,一定要明白,tags集合中的文檔和文件的tags陣列項目文檔中有沒有內在的聯繫是非常重要的。因此,如果您將標籤作爲項目的一部分保存,那麼除非您明確將其添加到tags集合中,否則這些標籤不會在tags集合中出現。

在發佈代碼的一些具體問題:

  1. 你需要你在ProjectSchema使用它之前定義TagSchema
  2. 您應該將ProjectSchema傳遞給mongoose.model呼叫,而不是Project
  3. 保持模式和模型名稱分離,因爲它不清楚代碼中的內容。
+0

是的,我想我基本上只是發現,由於你們的幫助,模式和模型之間是有區別的,謝謝!我會及時通知你的。 – 2013-03-05 15:44:46

1

藉助@ alawson421的代碼和一些數組魔法,它完美地工作,再次感謝@JohnnyHK向我展示了架構和模型之間的區別。繼承人的工作修復:

app.get('/tags.json', function(req, res) { 
    ProjectModel.find(function (err, projects) { 
     var taglist = []; 

     projects.forEach(function(project){ 
      //console.log(JSON.stringify(project.tags)); 
      project.tags.forEach(function(tag){ 
       console.log(tag); 
       taglist.push(tag.name); 
      }); 
     }); 

     res.json(taglist); 
    }); 
}); 

,輸出是:

[ 
    "test", 
    "meep", 
    "lalela", 
    "another test", 
    "ihopethisworks", 
    "noderocks", 
    "i am not a mongo", 
    "ice cream rocks" 
]