求助:waterline 链接mongodb 为何链接不上
发布于 8 年前 作者 lileilei 4225 次浏览 来自 问答

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);
});

});

3 回复

补充!! 但是我利用mongoose 链接测试时成功的!!!

同样在用waterline,给你一个我们的列子参考:

if (!this.dbOptions.adapters[this.config.db_type]) {
            this.dbOptions.adapters[this.config.db_type] = thinkRequire(`sails-${this.config.db_type}`);
        }
//数据源
        this.dbOptions = {
            adapters: {
                'mysql': thinkRequire('sails-mysql'),
				'mongo': thinkRequire('sails-mongo')
            },
            connections: {}
        };
		//数据源链接配置
        this.dbOptions.connections[this.adapterKey] = {
            adapter: this.config.db_type,
            host: this.config.db_host,
            port: this.config.db_port,
            database: this.config.db_name,
            user: this.config.db_user,
            password: this.config.db_pwd,
            charset: this.config.db_charset,
            wtimeout: 10,
            auto_reconnect: true,
            pool: true,
            connectionLimit: 30,
            waitForConnections: true
        };
/**
     * 连接池
     * @returns {*}
     */
    async setConnectionPool(){
        try{
            //closed connect for init
            THINK.INSTANCES.DB[this.adapterKey] && await this.close(this.adapterKey);
            //check adapters
            if (!this.dbOptions.adapters[this.config.db_type]) {
                return this.error(`adapters is not installed. please run 'npm install sails-${this.config.db_type}@0.11.x'`);
            }
            //load collections
            if(isEmpty(THINK.ORM[this.adapterKey])){
                return this.error('orm initialize faild. please check db config.');
            }
            let schema = THINK.ORM[this.adapterKey]['thinkschema'];
            for (let v in schema) {
                THINK.ORM[this.adapterKey].loadCollection(schema[v]);
            }
            //initialize
            let inits = promisify(THINK.ORM[this.adapterKey].initialize, THINK.ORM[this.adapterKey]);
            let instances = await inits(this.dbOptions).catch(e => this.error(e.message));
            THINK.INSTANCES.DB[this.adapterKey] = instances;
            return instances;
        }catch (e){
            return this.error(e);
        }
    }

@richenlin 谢谢。 找到问题了。。他娘的github文档上拷贝的例子。数据集合connection 明明可以指定default,实际却不能。我也不知道为何了

回到顶部