sailsjs still uses default database after changing it to mongodb

微笑、不失礼 提交于 2019-12-02 05:10:17

问题


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.


回答1:


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




回答2:


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.




回答3:


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'



回答4:


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



来源:https://stackoverflow.com/questions/28658299/sailsjs-still-uses-default-database-after-changing-it-to-mongodb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!