sails-orm-bug

when your sails version >= 0.12.2 ,and your config/connection.js adapter key is module ,such as :

1
2
3
4
5
6
7
8
devMongodb: {
module: 'sails-mongo',
host: process.env.MONGO_HOST || 'localhost',
port: process.env.MONGO_PORT || 57017,
user: process.env.MONGO_USER || '',
password: process.env.MONGO_PASS || '',
database: process.env.MONGO_DBNAME || 'koala'
}

it will appear this error “Cannot read property ‘config’ of undefined”. Beacause sails version >= 0.12.2 orm is Separated into independent modules “sails-hook-orm” , this module don’t support ‘module’ key as adapter.

you can solve it by two way:

  • change adapter key ‘module’ to ‘adapter’
  • rockback sails to lower version (<=0.12.1) , and delete module “sails-hook-orm”

when you config multi adapter and the adapter key is ‘adapter’ , sails <= 0.12.1 with orm haved bug :

origin code : https://github.com/balderdashy/sails/blob/v0.12.1/lib/hooks/orm/build-orm.js#L26

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
sails.log.verbose('Starting ORM...');
var waterline = new Waterline();
_.each(modelDefs, function loadModelsIntoWaterline(modelDef, modelID) {
sails.log.silly('Registering model `' + modelID + '` in Waterline (ORM)');
waterline.loadCollection(Waterline.Collection.extend(modelDef));
});

// Find all the connections used
var connections = _.reduce(sails.adapters, function getConnectionsInPlay(connections, adapter, adapterKey) {
_.each(sails.config.connections, function(connection, connectionKey) {
if (adapterKey === connection.adapter) {
connections[connectionKey] = connection;
}
});
return connections;
}, {});

fix this bug :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
sails.log.verbose('Starting ORM...');
var connectionsNameList = [];
var waterline = new Waterline();
_.each(modelDefs, function loadModelsIntoWaterline(modelDef, modelID) {
sails.log.silly('Registering model `' + modelID + '` in Waterline (ORM)');
connectionsNameList = connectionsNameList.concat(modelDef.connection);
waterline.loadCollection(Waterline.Collection.extend(modelDef));
});
connectionsNameList = _.uniq(connectionsNameList);
// Find all the connections used
var connections = _.reduce(sails.adapters, function getConnectionsInPlay(connections, adapter, adapterKey) {
_.each(sails.config.connections, function(connection, connectionKey) {
if (adapterKey === connection.adapter && _.indexOf(connectionsNameList,connectionKey) !== -1) {
connections[connectionKey] = connection;
}
});
return connections;
}, {});
{% if theme.baidu_push %} {% endif %}