javascript - 从 MongoDB 驱动程序调用 getUsers() 函数

标签 javascript node.js mongodb sails.js mongodb-query

我目前正在构建一个API应用程序,该应用程序使用Sailsjs(例如用户)检查状态并获取各种类型的数据库(即Mongo、MySQL)的信息,具体取决于用户输入。这是我正在处理的代码片段。本地主机只是我连接的测试数据库,但将来它将由用户提供。

      var mp = require('mongodb-promise');
      var MongoClient = require('mongodb');



  mp.MongoClient.connect("mongodb://@localhost:27017/test")
      .then(function(db){
          db.getUsers().then(function(users){
           res.ok(users);
         })
      })
   .fail(function(err) {
    console.log(err);
})

我正在尝试使用 Promise 来解决异步问题。我遇到的问题是它不起作用。它告诉我 Object[object object] 没有方法“getUsers”。我已经搜索过,似乎找不到有效的解决方案。

如果我将函数更改为以下内容,我会得到一些数据。

 mp.MongoClient.connect("mongodb://@localhost:27017/IMS")
     .then(function(db){
        db.stats().then(function(stats){
         return res.ok(stats);
        })
    })
   .fail(function(err) {
    console.log(err);
    dbObject.vipUp = false;
    }) 

我不确定问题是什么或如何解决。

最佳答案

您在这里所做的是使用 Node native 驱动程序方法来连接和检查数据库。事实上,在此 API 或任何其他 API 中“没有这样的方法”,如 .getUsers()

.getUsers() 函数只是一个“shell 助手”,基本上是这样实现的:

function (args) {
    var cmdObj = {usersInfo: 1};
    Object.extend(cmdObj, args);
    var res = this.runCommand(cmdObj);
    if (!res.ok) {
        var authSchemaIncompatibleCode = 69;
        if (res.code == authSchemaIncompatibleCode ||
                (res.code == null && res.errmsg == "no such cmd: usersInfo")) {
            // Working with 2.4 schema user data
            return this.system.users.find({}).toArray();
        }

        throw Error(res.errmsg);
    }

    return res.users;
}

所以你应该在这里看到的是,这通常包装一个“命令”形式,或者以其他方式回退到与 MongoDB 2.4 的兼容性来查询当前数据库上的 system.users 集合.

因此,您需要使用.command(),而不是调用不存在的方法。方法替代:

mp.MongoClient.connect("mongodb://@localhost:27017/test")
      .then(function(db){
          db.command({ "usersInfo": 1}).then(function(users){
           res.ok(users);
         })
      })
   .fail(function(err) {
    console.log(err);
})

或者在连接到 MongoDB 2.4 实例的情况下,然后从 .collection() 获取:

mp.MongoClient.connect("mongodb://@localhost:27017/test")
      .then(function(db){
          db.collection('system.users').find().toArray().then(function(users){
           res.ok(users);
         })
      })
   .fail(function(err) {
    console.log(err);
})
无论如何,您确实应该在应用程序的其他位置建立数据库连接(或重新使用来自另一个存储的底层驱动程序连接),然后在已建立的连接上调用方法。这总是比根据您要检索的信息的请求创建连接更好。

此外,最近版本的 node native driver开箱即用的支持 promise 。因此,可能不需要进行任何其他配置,具体取决于您打算如何使用它。

关于javascript - 从 MongoDB 驱动程序调用 getUsers() 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32063113/

相关文章:

mongodb - 为什么要计数查询抓取文件?

node.js - 使用或不使用 'new' 关键字创建 Mongoose 模式?

javascript - aframe 不渲染映射到 Canvas 的 Lottie json 纹理,但可以在 Three.js 中使用

php - 如何使用 PHP echo 函数在 JavaScript 中定义变量?

node.js - 如何在 VS Code 中调试 React Tape 单元测试

javascript - 哈皮 : include a js custom library

python - 升级后无法连接到 MongoDB?

javascript - 单击时更改背景颜色

javascript - 避免 Veracode CWE-80 : Improper Neutralization of Script-Related HTML in jquery htm() method

node.js - 在哪里可以找到 Angular CLI 7.3.9 默认生成的 webpack 设置文件,以便我可以基于它构建我的覆盖?