0
我使用Meteor.JS和pcel:mysql從該方法獲取mysql結果並將其傳遞給幫助程序。流星JS - 嵌套函數返回值不正確
在我的方法中,該方法執行得很好,並使用connection.query我能夠在服務器端控制檯中記錄mysql行。
這是我的代碼如下所示:
if (Meteor.isClient) {
Meteor.call('mysqltestcall1', function(error, result){
Session.set('myMethodResult', result);
});
Template.hello.helpers({
data2: function(){
return Session.get('myMethodResult');
}
});
}
if (Meteor.isServer) {
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '',
database : 'leaderboard'
});
connection.connect();
Meteor.methods({
'mysqltestcall1': function(){
var returnresult = 'test value';
connection.query('SELECT * FROM players', function(err, rows, fields) {
if (err) throw err;
console.log(rows);
returnresult = rows;
});
return returnresult;
}
});
}
我得到的值作爲「測試值」,而不是行JSON數據。誰能幫我嗎。
我的事情我沒有正確使用該變量是在嵌套函數。
裹此Meteor.call成Template.hello.onCreated功能 – Sindis
我曾嘗試這樣的: \t \t Template.hello.onCreated(函數(){.......}); 但結果是一樣的。 我認爲這是我們的變量沒有在嵌套函數中得到更新的東西。 – Manu