2016-02-05 51 views
1

我正在嘗試使用Angular-Meteor中的集合中的數據,但到目前爲止,我無法訪問它。無法訪問Angular-Meteor中的集合

的lib/collections.js我定義集合:

UserMeta = new Mongo.Collection('userMeta'); 

服務器/ publish.js其出版:

Meteor.publish('userMeta', function() { 
    return UserMeta.find(); 
}); 

,並在我的客戶端代碼客戶機/腳本/controllers/settings.controller.js我訂閱:

angular 
    .module('App') 
    .controller('SettingsCtrl', SettingsCtrl); 

function SettingsCtrl($scope, $reactive) { 
    $reactive(this).attach($scope); 
    this.subscribe('userMeta'); 
    //... 
} 

我發現,似乎有多種方式認購(即使從角流星官方教程使用不一致),但我已經決定要使用最新的語法v.1.3.2:Subscribe API

但是,如果我想查看集合的全部內容,它返回一個空數組:

console.log(UserMeta.find().fetch()); // =[] 

或者:

console.log(UserMeta.findOne()); // =undefined 

但是,如果我在瀏覽器的客戶端控制檯中運行這些命令,返回預期的結果。

有人請給我一個簡短的例子,我如何能與我的收藏?我已經習慣了(純粹的)Meteor處理這個問題的方式,我很困惑它在Angular-Meteor中看起來並不那麼簡單。

回答

0

使用

Meteor.Methods 

在您的服務器端調用嘗試

Meteor.methods({ 
getUserMeta: function() { 
var data = UserMeta.find({}).fetch(); 
return data; 
} 

當您在CONSOLE.LOG它不是用集中的所有呼叫使用

Meteor.call('getUserMeta',function (err, data) { 
    if (!err) { 
    Console.log(data); 
    } else { 
    console.log("error"); 
    } 
}); 
0

在服務器端這個方法尚未準備好,並且沒有任何數據。 您可以在幫助程序中使用console.log,或者您應該檢查收集是否準備好像這樣:

// Client-side 
var subs = Meteor.subscribe('lastMsgRead', Meteor.userId()); 
Meteor.autorun(function() { 
    if (subs.ready()) { ... } 
});