如何使用sailsjs v0.10连接mongodb?
现在使用sailsjs v0.10。 configurationconnections.js和models.js并将其更改为连接:'localMongodbServer',安装npm install sails-mongo。
事后所有这显示错误
var des = Object.keys(dbs[collectionName].schema).length === 0 ? ^ TypeError: Cannot read property 'schema' of undefined at Object.module.exports.adapter.describe (app1_test/node_modules/sails-mongo/lib/adapter.js:70:48)
如果将collections.js更改为adapter.js会显示错误
[err] In model (model1), invalid connection :: someMongodbServer [err] Must contain an `adapter` key referencing the adapter to use.
没有看到代码,我只能假设一些事情。
- 你正在开始一个新的sailsjs v0.10项目
- 你没有正确地configuration你的configuration。
如果不是这样,让我知道,所以我可以适当地更新答案。
我有一个v0.10的样板,里面有几件东西,所以你可以看看它是如何完成的。 在这里看到回购
connections.js
是相应的文件名,它在0.10
被更改。
首先确保安装了sails-mongo。
#From your project root run npm install sails-mongo --save
接下来,您需要定义您的连接,并告诉sails缺省情况下为模型使用哪个适配器。 这里是一个connections.js
和models.js
应该是什么样子的例子。
connections.js
module.exports.connections = { mongodb: { adapter : 'sails-mongo', host : 'localhost', port : 27017, user : '', password : '', database : 'yourdevdb' } }
models.js
module.exports.models = { // Your app's default connection. // ie the name of one of your app's connections (see `config/connections.js`) // // (defaults to localDiskDb) connection: 'mongodb' };
您还可以在config/local.js
指定您的连接,以避免将敏感数据提交到您的存储库。 这是你如何做到的。
你不需要指定所有的内容,因为local.js
会覆盖connections.js
定义的内容。
local.js
module.exports = { connections: { mongodb: { host : 'localhost', port : 27017, user : '', password : '', database : 'yourdevdb' } } }
您甚至可以在单个模型中定义适配器,例如,您需要使用单个模型来与不同的数据库types进行对话。
你可以通过指定adapter:
在你的模型中
module.exports = { adapter: 'myothermongodb', }, config: { user: 'root', password: 'thePassword', database: 'testdb', host: '127.0.0.1' },
如果您正在使用v0.10,则需要在Github上从v0.10
分支安装v0.10
sails-mongo
,因为Waterline适配器API已在v0.10中进行了更改。 在你的package.json
"sails-mongo": "https://github.com/balderdashy/sails-mongo/archive/v0.10.tar.gz"
然后运行npm install
。
在config/connections.js
你应该有描述的MongoDB适配器,并且在config/models.js
必须引用这个适配器。
就是这样, sails lift
应该在那之后工作。