sailsjs still uses default database after changing

2019-02-26 10:28发布

I am new to sailsjs and currently working on project that uses sailsjs. I want to change the default database to mongodb. I did it in config/local.js like following

connections: {
    'defaults': 'mongo',
    mongo: {
       module: 'sails-mongo',
       host: 'localhost',
       user: '',
       password: '',
       database: 'dbName',
       schema: true
    }
}

After starting mongodb and tried to create some data using application(that I am working on), and then when I check that in mongodb using mongodb command line tool. It finds no data there and when I check that in application it loads all of the data from database. That means it is still using the default database where that data is stored.

I am using sailsjs version 0.11.0.

4条回答
祖国的老花朵
2楼-- · 2019-02-26 10:38

Just Run npm install sails-mongo

Put below configuration on /config/connections.js

 mongodbServer: {
    adapter: 'sails-mongo',
    host: 'localhost',
    port: 27017,
    database: 'test',
    schema:true
  }

Mention the mongodbServer as like below in /config/models.js

connection: 'mongodbServer',
migrate: 'alter'
查看更多
甜甜的少女心
3楼-- · 2019-02-26 10:47

There can be some issues with multiple named adapters.

It's best to completely name everything differently (aka MongoDev, MongoProd) and if your issue is separating dev and production, but all your connection and default model params in the config/env/ - production.js - development.js

You should check out the following links

https://github.com/balderdashy/sails/issues/939

Handling database environment configuration in Sails.js

查看更多
Animai°情兽
4楼-- · 2019-02-26 10:51

Is your adapter defined? In the connection.js it would be

localDiskDb: {
 adapter: 'sails-mongo'
},
someMongodbServer: {
    adapter: 'sails-mongo',
    host: 'localhost',
    port: 27017,
},

I'm also new to sail but I'm pretty sure you don't need to touch to local.js

查看更多
不美不萌又怎样
5楼-- · 2019-02-26 10:55

You need to change the connection used by your models in the models.js file, located in the config folder. Set:

connection: 'mongo'

In general, you can define your adapters in the connections.js file, or in your local.js file. Local.js takes precedence, and is mainly to protect sensitive configuration information (passwords, etc.) since it doesn't get uploaded with git. You still need to set which adapter to actually use in the models.js file.

查看更多
登录 后发表回答