如何使用sailsjs v0.10与mongodb连接? [英] How to connect with mongodb using sailsjs v0.10?

查看:87
本文介绍了如何使用sailsjs v0.10与mongodb连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在使用sailsjs v0.10. 配置connections.js和models.js并将其更改为connection:'localMongodbServer',安装了npm install sails-mongo.

Now Using sailsjs v0.10 . Configure connections.js and models.js and change it to connection: 'localMongodbServer' ,installed 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则显示错误

If change collections.js to adapter.js shows error

  [err] In model (model1), invalid connection :: someMongodbServer
  [err] Must contain an `adapter` key referencing the adapter to use.

推荐答案

没有代码,我只能承担几件事.

Without seeing code, i can only assume a few things.

  1. 您正在启动一个新的sailsjs v0.10项目
  2. 您没有正确设置配置.

如果不是这种情况,请告诉我,以便我可以适当地更新答案.

If this isnt the case, let me know so i can update the answer appropriately.

我有一个v0.10的样板,其中包含了一些内容,因此您可以看到它是如何完成的.在此处

I have a boilerplate for v0.10 that has a few things baked into it, so you can see how its done. See that repo here

connections.js是适当的文件名,已在0.10中更改.

connections.js is the appropriate filename, it was changed in 0.10.

首先确保安装了sails-mongo.

First make sure sails-mongo is installed.

#From your project root run
npm install sails-mongo --save

接下来,您需要定义连接,并告诉Sails默认情况下要用于模型的适配器.这是connections.jsmodels.js外观的示例.

Next you need to define your connection, and tell sails what adapter to use for models by default. Here is an example of what connections.js and models.js should look like.

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.
  // i.e. the name of one of your app's connections (see `config/connections.js`)
  //
  // (defaults to localDiskDb)
  connection: 'mongodb'
};


您还可以在config/local.js中指定连接,以避免将敏感数据提交到存储库.这就是你的方法.


You can also specify your connections in config/local.js to avoid commiting sensitive data to your repository. This is how you do it.

您不需要指定所有内容,因为local.js会覆盖connections.js中定义的内容,而Sails也会将它们组合在一起.

You dont need to specify all of the contents, as local.js will override whats defined in connections.js Sails will also combine them.

module.exports = {
  connections: {
      mongodb: {
        host      : 'localhost',
        port      : 27017,
        user      : '',
        password  : '',
        database  : 'yourdevdb'
      }
  }
}

您甚至可以在单个模型中定义适配器,例如在需要单个模型与其他数据库类型进行通信的情况下.

You can even define your adapter in a single model, for instances where you need a single model to talk to a different database type.

您可以通过在模型中指定adapter:来执行此操作.

You do this by specifying the adapter: in your model..

module.exports = {
  adapter: 'myothermongodb',
},
config: {
  user: 'root',
  password: 'thePassword',
  database: 'testdb',
  host: '127.0.0.1'
},

这篇关于如何使用sailsjs v0.10与mongodb连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆