我有一個位置模型和一個記錄器模型。我希望能夠將兩個數據集的所有數據傳遞給我的視圖模型。我該如何訪問它們,但因爲我覺得他們不是在範圍,因爲我越來越不確定的錯誤,因爲我打電話「所有」如何從控制器訪問多個模型
https://gist.github.com/3998302
var Main = function() {
this.index = function (req, resp, params) {
var self = this;
var data = {};
geddy.model.Locations.all(function(err, locations) {
data.locations = locations;
geddy.model.Recorders.all(function(err, recorders) {
data.recorders = recorders;
self.respond({params: params, data: data}, {
format: 'html'
, template: 'app/views/locations/index'
}
});
}););
};
};
exports.Main = Main;
錯誤片段:
timers.js:103
if (!process.listeners('uncaughtException').length) throw e;
^
TypeError: Cannot call method 'all' of undefined
at index (G:\code\PeopleTracker\app\controllers\main.js:23:24)
at controller.BaseController._handleAction.callback (C:\Users\Chris\AppData\Roaming\npm\node_modules\geddy\lib\base_
controller.js:387:22)
正確:JavaScript並不像Perl那樣「自動化」對象。將一個屬性賦值給一個未定義的變量不會自動將該變量轉換爲對象引用。 – ebohlman
我改變了這一點,它仍然給我一個錯誤。 timers.js:103 if(!process.listeners('uncaughtException')。length)throw e; ^ TypeError:無法在index(G:\ code \ PeopleTracker \ app \ controllers \ main.js:23:24)處調用未定義的 方法'all' at controller.BaseController._handleAction.callback(C:\ Users \ Chris \ AppData \ Roaming \ npm \ node_modules \ geddy \ lib \ base_ controller.js:387:22) – cmp