2016-01-07 43 views
1

我是新來的流星框架 我想從取單場流星

AccountNames = new Mongo.Collection("AccountTypeMaster"); 

我創建使用

db.createCollection("AccountTypeMaster") 

this.helpers({ 
    AccountNames:() => { 
     return AccountNames.find({}, {fields: {name: 1}});  
    } 
}); 

集合使用上面的查詢我」集合取單m無法從集合中獲取單個字段「名稱」。 我現在確定我的代碼有什麼問題。

+1

你需要調用[**'取()'**](http://docs.meteor.com/#/full/fetch)上的光標到所有匹配的文件作爲一個數組。 – chridam

+1

'db.createCollection(「AccountTypeMaster」)'在流星應用中是不正確的語法。我建議遵循[官方教程](https://www.meteor.com/tutorials/blaze/creating-an-app)。 –

回答

0

你需要改變你如何實例化你的集合。正確的Meteor語法應該是:

AccountNames = new Mongo.Collection("AccountTypeMaster"); 

還需要將助手附加到模板。請記住,助手只能在客戶端代碼上運行。

if (Meteor.isClient) { 
    // This code only runs on the client 
    Template.body.helpers({ 
    tasks: function() { 
     return AccountNames.find({}, { fields: { name: 1 } }); 
    } 
    }); 
} 
0

在您的項目中創建客戶端文件夾並將客戶端代碼放入該文件夾中。 To create collection in mongodb

Template.name.helpers({ 
    fun: function() { 
    return AccountNames.find({},{name: 1}).fetch();  

})