2016-04-05 49 views
0

我在Http main.js中進行調用,並將此返回給我的控制檯「Uncaught TypeError:無法讀取null的屬性'set' 無法從中檢索信息我的http調用在函數本身之外。 感謝您的幫助。流星:如何使用來自模板中的Http調用的數據

Template.prod.onCreated(function prodOnCreated() { 
var tmpl = this; 
tmpl.data = new ReactiveVar; 

HTTP.call('GET', 'https://localhost:9002/rest/v2/electronics/products/search/?query=:price-asc:category:575', {}, function(error, response) { 
if (error) 
console.log(error); 
else { 
console.log(response.data.products[0].code); // that works: display "2140285" 
tmpl.data.set(response.data);// Doesn't work: message in the console "Uncaught TypeError: Cannot read property 'set' of null" 
} 
}); 
}); 


Template.prod.helpers({ 
data: function() { 
console.log('Hi you are in the data function'); 
return Template.instance().data.get(); 
} 
}); 

控制檯顯示:

main.js:40 Uncaught TypeError: Cannot read property 'set' of null(anonymous function) @ main.js:40(anonymous function) @ httpcall_client.js:83(anonymous function) @ underscore.js?hash=8de51f9…:784xhr.onreadystatechange @ httpcall_client.js:172

+2

'tmpl.data = new ReactiveVar();'參見parancehesis – Sasikanth

+0

Hi @Sasikanth我改正了它,並且我有同樣的錯誤 – Menahem770

回答

0

首先,您需要使用添加包下面

meteor add reactive-var 

,那麼你可以初始化reactivevar像

tmpl.data = new ReactiveVar(); 
+0

這個包已經包含在流星的1.3版本中: 在JS的頭文件中:import { }'流星/響應變種'的ReactiveVar; – Menahem770

+0

它應該是從'meteor/reactive-var''導入{ReactiveVar}的權利? – Sasikanth

+0

事實上,反應式變量工作,例如: Template.hello.onCreated(函數helloOnCreated(){ //計數器從0開始 this.counter = new ReactiveVar(5); console.log(this.counter.get ()); //它的工作原理:display 5 }); – Menahem770

相關問題