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

时间:2018-08-04 10:05:33

标签: node.js mongodb express

尝试通过发布获取数据时,我收到“ MongoError:集合名称必须为字符串”。这是我得到的错误:-

let apiUrl = "https://url/"
fetch(apiUrl)
 .then((response) => response.json())
 .then((jsonData=>{
   console.log(jsonData);
})
.catch((err)=>{
  console.log("err",err);
}

这里是代码行号:-

"
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

"

请让我知道我做错了。

1 个答案:

答案 0 :(得分:0)

采用如下所示的连接URL,其中 test 是您要连接的数据库

mongodb://localhost:27017/test

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

dbConnection.db = db;

要执行以下操作:

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