node.js 如何使用sailsjs v0.10连接mongodb?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21598339/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:48:34  来源:igfitidea点击:

How to connect with mongodb using sailsjs v0.10?

node.jsmongodbsails.js

提问by tajuddin

Now Using sailsjs v0.10 . Configure connections.js and models.js and change it to connection: 'localMongodbServer' ,installed npm install sails-mongo.

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

Aftet all this it shows error

在这一切之后它显示错误

 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)

If change collections.js to adapter.js shows error

如果将 collections.js 更改为 adapter.js 显示错误

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

回答by gorelative

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

没有看到代码,我只能假设一些事情。

  1. You're starting a new sailsjs v0.10 project
  2. You dont have your configuration setup properly.
  1. 你正在开始一个新的sailsjs v0.10 项目
  2. 您的配置设置不正确。

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

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



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

我有一个 v0.10 的样板,里面有一些东西,所以你可以看到它是如何完成的。在此处查看该回购

connections.jsis the appropriate filename, it was changed in 0.10.

connections.js是适当的文件名,它在0.10.

First make sure sails-mongo is installed.

首先确保安装了sails-mongo。

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

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.jsand models.jsshould look like.

接下来您需要定义您的连接,并告诉sails 默认情况下为模型使用什么适配器。这是一个什么样的例子connections.jsmodels.js应该像。

connections.js

连接.js

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

models.js

模型.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'
};


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

您还可以指定您的连接config/local.js以避免将敏感数据提交到您的存储库。这就是你如何做到的。

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

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

local.js

本地.js

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.

您甚至可以在单个模型中定义您的适配器,例如您需要单个模型来与不同的数据库类型对话。

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

您可以通过adapter:在模型中指定..

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

回答by bredikhin

If you are working with v0.10, you need to install sails-mongofrom v0.10branch on Github, 'cause the Waterline adapter API was changed in v0.10. In your package.jsonput

如果您使用的是 v0.10,则需要sails-mongov0.10Github 上的分支安装,因为在 v0.10 中更改了 Waterline 适配器 API。在你的package.json投入

"sails-mongo": "https://github.com/balderdashy/sails-mongo/archive/v0.10.tar.gz"

then run npm install.

然后运行npm install

In config/connections.jsyou should have MongoDB adapter described, and in your config/models.jsthis adapter must be referenced.

config/connections.js你应该有 MongoDB 适配器描述,并且在你的config/models.js这个适配器中必须被引用。

That's it, sails liftshould work after that.

就是这样,sails lift在那之后应该可以工作。