balderdashy / sails

Realtime MVC Framework for Node.js
https://sailsjs.com
MIT License
22.82k stars 1.95k forks source link

cannot initialize waterline because object has no method apply #5377

Closed traskat closed 5 years ago

traskat commented 9 years ago

Hi,

i am running in an TypeError through the execution of th function initialize.

this is the error:

TypeError: Object [object Object] has no method 'apply' at new child (sources/wallride/node_modules/waterline/lib/waterline/utils/extend.js:17:39) at initialize (/sources/wallride/node_modules/waterline/lib/waterline/collection/loader.js:37:10) at loadCollection (sources/wallride/node_modules/waterline/lib/waterline.js:112:29) at sources/wallride/node_modules/waterline/node_modules/async/lib/async.js:162:20 at sources/wallride/node_modules/waterline/node_modules/async/lib/async.js:230:13 at _arrayEach (sources/wallride/node_modules/waterline/node_modules/async/lib/async.js:81:9) at _each (/sources/wallride/node_modules/waterline/node_modules/async/lib/async.js:72:13) at Object.async.forEachOf.async.eachOf (sources/wallride/node_modules/waterline/node_modules/async/lib/async.js:229:9) at Object.async.forEach.async.each (sources/wallride/node_modules/waterline/node_modules/async/lib/async.js:206:22) at Array.async.auto.loadCollections as 0 at sources/wallride/node_modules/waterline/node_modules/async/lib/async.js:590:38 at _arrayEach (sources/wallride/node_modules/waterline/node_modules/async/lib/async.js:81:9) at Object.async.auto (sources/wallride/node_modules/waterline/node_modules/async/lib/async.js:552:9) at Waterline.initialize (sources/wallride/node_modules/waterline/lib/waterline.js:121:9) at sources/wallride/app.js:102:13 at Layer.handle as handle_request

i am using it like in this example https://github.com/balderdashy/waterline/blob/master/example/express/express-example.js

my code looks like this:

        var connection = new Waterline();
    config.adapters.mysql = sailsMysql;

    var conf = {
        adapters: {
            mysql: sailsMysql
        },
        connections: {
            mysqlRemote: {
                host: 'localhost',
                username: 'tester',
                password: 'testing',
                database: 'atabase',
                adapter: 'mysql'
            }
        }
    }
    var category = new Waterline.Collection.extend({
        identity: 'category',
        connection: 'mysqlRemote',
        tablename: 'category',
        migrat: 'alter',
        schema: true,
        attributes: {
            id: {
                type: 'integer',
                primaryKey: true
            },
            post_id: {
                type: 'integer',
                required: true
            },
            category_name: {
                type: 'string',
            }
        }
    });

    connection.loadCollection(category);

    connection.initialize(conf, function(err, models) {
        if (err) {
            console.log('DB ERROR: ', err);
        }

        req.models = models.collections;
        req.connections = models.connections;
    });

i am using the waterline version 0.10.26 the sails-mysql module is installed. any idea why this typeerror happens?

thanks

tjwebb commented 9 years ago
config.adapters.mysql = sailsMysql;
var conf = {
        adapters: {
            mysql: sailsMysql
        },

Where is sailsMysql defined?

traskat commented 9 years ago

it is defined above, sorry copy & paste fault:

    var Waterline = require('waterline');
    var sailsMysql = require('sails-mysql');

    var connection = new Waterline();
    config.adapters.mysql = sailsMysql;

    var conf = {
        adapters: {
            mysql: sailsMysql
        },
        connections: {
            mysqlRemote: {
                host: 'localhost',
                username: 'tester',
                password: 'testing',
                database: 'database',
                adapter: 'mysql'
            }
        }
    }
    var category = new Waterline.Collection.extend({
        identity: 'category',
        connection: 'mysqlRemote',
        tablename: 'category',
        migrat: 'alter',
        schema: true,
        attributes: {
            id: {
                type: 'integer',
                primaryKey: true
            },
            post_id: {
                type: 'integer',
                required: true
            },
            category_name: {
                type: 'string',
            }
        }
    });

    connection.loadCollection(category);

    connection.initialize(conf, function(err, models) {
        if (err) {
            console.log('DB ERROR: ', err);
        }

        req.models = models.collections;
        req.connections = models.connections;
    });

this line is not important, was only a test config.adapters.mysql = sailsMysql; from the config file

traskat commented 9 years ago

find found the problem.

for the model declaration i have to use this

var category = Waterline.Collection.extend({

instead of: var category = new Waterline.Collection.extend({

thanks