Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetching extendsTo models with autoFetch fails #283

Closed
colegleason opened this issue Aug 5, 2013 · 7 comments
Closed

Fetching extendsTo models with autoFetch fails #283

colegleason opened this issue Aug 5, 2013 · 7 comments

Comments

@colegleason
Copy link

I'm trying to use autoFetch on a model with extensions, but I get this error:

/Users/cole.gleason/code/tantrum-client/node_modules/orm/lib/Model.js:246
            throw ErrorCodes.generateError(ErrorCodes.MISSING_CALLBACK, "Missing Model.
                             ^
Error: Missing Model.get() callback
    at Object.defineProperty.value (/Users/cole.gleason/code/tantrum-client/node_modules/orm/lib/ErrorCodes.js:12:13)
    at Function.model.get (/Users/cole.gleason/code/tantrum-client/node_modules/orm/lib/Model.js:246:21)
    at Object.defineProperty.value (/Users/cole.gleason/code/tantrum-client/node_modules/orm/lib/Associations/Extend.js:91:23)
    at autoFetchInstance (/Users/cole.gleason/code/tantrum-client/node_modules/orm/lib/Associations/Extend.js:179:36)
    at Object.exports.autoFetch (/Users/cole.gleason/code/tantrum-client/node_modules/orm/lib/Associations/Extend.js:68:3)
    at createInstance (/Users/cole.gleason/code/tantrum-client/node_modules/orm/lib/Model.js:124:23)
    at autoFetchDone (/Users/cole.gleason/code/tantrum-client/node_modules/orm/lib/Associations/Many.js:94:11)
    at /Users/cole.gleason/code/tantrum-client/node_modules/orm/lib/Associations/Many.js:403:10
    at Statement.chain.all (/Users/cole.gleason/code/tantrum-client/node_modules/orm/lib/ChainFind.js:125:13)

I'm enabling the autoFetch in the model like this:

     var Endpoint = models.endpoint = db.define('endpoint', {                                                                                                                                
         name: {type: 'text', required: true},                                                                                                                                               
         protocol: {type: 'text', required: true, defaultValue: 'HTTP'},                                                                                                                     
         active: {type: 'boolean', required: true, defaultValue: 'true'}                                                                                                                     
     }, {                                                                                                                                                                                    
         autoFetch: true                                                                                                                                                                  
     });                                                                                                                                                                                     

     models.endpointRemote = Endpoint.extendsTo('remote', {                                                                                                                                  
         host: {type: 'text', required: true},                                                                                                                                               
         port: {type: 'number', required: true}                                                                                                                                              
     });                                                                                                                                                                                     
     models.endpointLocal = Endpoint.extendsTo('local', {                                                                                                                                    
         listen: {type: 'number', required: true, defaultValue: '0.0.0.0'},                                                                                                                  
         port: {type: 'number', required: true}                                                                                                                                              
     });   

It fails during a get, which I call like this:

    Endpoint.get(req.params.id, function(err, item) { . . . ); 
@dresende
Copy link
Owner

dresende commented Aug 7, 2013

Are you sure it's on that line? The exception is about a missing callback in a Model.get call. Please show us some more of your code surrounding that call.

@colegleason
Copy link
Author

This is the entire Express request handler. I can't seem to increase the stack trace size at all, unfortunately.

exports.get = function(req, res) {                                                                                                                                                            
    var Endpoint = req.models.endpoint;                                                                                                                                                       
    Endpoint.get(req.params.id, function(err, item) {                                                                                                                                         
        console.log(item);                                                                                                                                                                    
        if (err) {                                                                                                                                                                            
            common.handleError(res, err);                                                                                                                                                     
        } else {                                                                                                                                                                              
            res.body = item;                                                                                                                                                                  
            common.respondJSON(res);                                                                                                                                                          
        }                                                                                                                                                                                     
    });                                                                                                                                                                                       
};    

@colegleason
Copy link
Author

Maybe this line is the issue? https://github.com/dresende/node-orm2/blob/master/lib/Associations/Extend.js#L179

Shouldn't that only be called with the callback argument and not the options object there?

@Fauntleroy
Copy link

I'm having the same exact issue when I try to use autoFetch on an extendsTo model. Here's what I've got...

var async = require('async');
var orm = require('orm');
var slug = require('slugg');
var EventEmitter = require('events').EventEmitter;

const TIMER_INTERVAL = 3000;

module.exports = function( db ){

    var Room = db.define( 'room', {
        title: {
            type: 'text',
            required: true,
            unique: true
        },
        slug: String,
        subtitle: {
            type: 'text',
            defaultValue: 'A great place to share with friends!'
        },
        active: Boolean,
        'private': Boolean
    }, {
        hooks: {...},
        methods: {...}
    });

    ...

    Room.Player = Room.extendsTo( 'player', {
        elapsed: {
            type: 'number',
            defaultValue: 0
        },
        duration: Number,
        order: {
            type: 'enum',
            values: ['normal','shuffle'],
            defaultValue: 'normal'
        }
    }, {
        autoFetch: true,
        hooks: {...},
        methods: {...},
        reverse: 'room'
    });

    return Room;

};

Elsewhere... (after all models are loaded, the other associations work)

module.exports = function( db ){

    var User = db.models.user;
    var Item = db.models.item;
    var PlaylistItem = db.models.playlist_item;
    var Room = db.models.room;

    // PlaylistItem
    PlaylistItem.hasOne( 'item', Item );

    PlaylistItem.hasOne( 'owner', User, {
        reverse: 'adds'
    });

    PlaylistItem.hasOne( 'influencer', User );

    // Room
    Room.hasOne( 'creator', User, {
        reverse: 'rooms'
    });

    Room.hasMany( 'items', PlaylistItem );

    Room.Player.hasOne( 'item', PlaylistItem );

};

@dresende
Copy link
Owner

Isn't this a dup of #323 ? If it is, I added a fix for it.

@colegleason
Copy link
Author

Looks like it, although I no longer have the original code to test with.

@dresende
Copy link
Owner

Ok, reopen if you stumble on it again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants