代码之家  ›  专栏  ›  技术社区  ›  Rishabh Agrawal

MongoError:集合名称必须是字符串

  •  0
  • Rishabh Agrawal  · 技术社区  · 6 年前

    在尝试通过post获取数据时,我得到了“mongoerror:collection name must be a string”。以下是我得到的错误:

    "
    MongoError: collection name must be a String
        at checkCollectionName (/home/rishabh/test/node_modules/mongodb/lib/utils.js:99:11)
        at new Collection (/home/rishabh/test/node_modules/mongodb/lib/collection.js:101:3)
        at Db.collection (/home/rishabh/test/node_modules/mongodb/lib/db.js:335:26)
        at Object.queries.getPlayersList (/home/rishabh/test/dbConnection/queries.js:207:24)
        at playersList (/home/rishabh/test/lib/MonthlyLeaderboard.js:38:11)
        at /home/rishabh/test/node_modules/async/lib/async.js:760:23
        at /home/rishabh/test/node_modules/async/lib/async.js:166:37
        at fn (/home/rishabh/test/node_modules/async/lib/async.js:746:34)
        at /home/rishabh/test/node_modules/async/lib/async.js:1213:16
        at /home/rishabh/test/node_modules/async/lib/async.js:166:37
    
    "
    

    这是代码行编号:

    queries.getPlayersList = function(collectionName, query, callback) {
    db().db('test').collection(collectionName).find(query.id).sort(query.sortBy).skip(query.skip).limit(parseInt(query.limit) ).toArray(function (err, result) {
    
            callback(err, result);
        })
    }
    

    请告诉我我做错了什么。

    1 回复  |  直到 6 年前
        1
  •  0
  •   cheekujha    6 年前

    连接URL如下所示 测试 数据库 您正在连接到:

    mongodb://localhost:27017/test
    

    在连接代码中,数据库实例存储为

    dbConnection.db = db;
    

    要查找,请执行以下操作:

    dbConnection.db.collection(CollectionName).find(query).sort(sortBy).toArray((err, docs) => {
        console.log('----docs----', docs);
    })