mongod 我再命令行中mongo -》 是可以链接成功的
mongod --dbpath d:\test 启动服务
然后执行 node watertest.js 报错:
Error: The connection default specified in user does not exist!
watertest.js 代码:
/**
- 演示 waterilne 的使用 */
var Waterline = require(‘waterline’); var mysqlAdapter = require(‘sails-mysql’); var mongoAdapter = require(‘sails-mongo’);
// 适配器 var adapters = { mongo: mongoAdapter, mysql: mysqlAdapter, default: ‘mongo’ };
// 连接 var connections = { mongo: { adapter: ‘mongo’, url: ‘mongodb://localhost/mydb’ } };
// 数据集合 var User = Waterline.Collection.extend({ identity: ‘user’, connection: ‘default’, schema: true, attributes: { username: { type: ‘string’, // 校验器 required: true } }, // 生命周期回调 beforeCreate: function(value, cb){ value.createTime = new Date(); console.log(‘beforeCreate executed’); return cb(); } });
var orm = new Waterline();
// 加载数据集合 orm.loadCollection(User);
var config = { adapters: adapters, connections: connections }
orm.initialize(config, function(err, models){ if(err) { console.error(‘orm initialize failed.’, err); return; }
// console.log('models:', models);
models.collections.user.create({username: 'Sid'}, function(err, user){
console.log('after user.create, err, user:', err, user);
});
});