0

I have a Locations model and a Recorders model. I want to be able to pass all of the data for both data sets to my view model. How can I access them though because I think they're not in scope since I'm getting undefined errors because I'm calling 'all'

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;

Error snippet:

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)
cmp
  • 1,543
  • 2
  • 11
  • 18

2 Answers2

0

So it looks like you're initializing the data variable to 'undefined'. Try data = {} instead. If that doesn't fix it, I'll do some troubleshooting with you.

EDIT

If that doesn't do it for you, try installing geddy again:

npm uninstall -g geddy && npm install -g geddy

If that doesn't do it, make sure that your DB is actually running, make sure that the models are defined (try geddy console to check your models), and make sure that you're on the latest stable version of node.

Techwraith
  • 1,298
  • 1
  • 10
  • 12
  • Correct: JavaScript doesn't "autovivify" objects the way that, say, Perl does. Assigning a property to an `undefined` variable doesn't automatically turn the variable into an object references. – ebohlman Nov 02 '12 at 09:40
  • I changed that and it still gives me an error. 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) – cmp Nov 02 '12 at 15:10
0

Very late to the party, but I believe you can just call

geddy.model.Locations.all(function(err, locations) {
    geddy.model.Recorders.all(function(err, recorders) {
        var data = {};
        data.locations = locations;
        data.recorders = recorders;
        self.respond({params: params, data: data}, {
        format: 'html'
        , template: 'app/views/locations/index'
        }
    });
}););

You could also have the respond say

self.respond({params: params, locations: locations, recorders: recorders});

but if you want all of that data available from the data literal you need it defined in the lowest scope callback. The callbacks can read above their scope but they cannot write above it.

Ganksy
  • 48
  • 1
  • 2
  • 8