【发布时间】:2021-12-25 04:26:34
【问题描述】:
下面是我的代码,clientNames 是一个字符串数组。
function GetCollectionAllowance(clientNames) {
// Retrieves client names from DB and adds to Array
MongoClient.connect('x', function (err, client) {
if (err) throw err
// DB Name
var db = client.db('client-time')
clientNames.forEach((element) => {
db.collection(element).find( {}, {allowance:1} )
.then(function(allowance) {
console.log(allowance); // Use this to debug
})
})
})
}
这是我收到的错误 -
/home/nolwag/Node/toggl/routes/index.js:229
.then(function(allowance) {
^
TypeError: db.collection(...).find(...).then is not a function
at /home/nolwag/Node/toggl/routes/index.js:229:16
at Array.forEach (<anonymous>)
at /home/nolwag/Node/toggl/routes/index.js:225:25
at /home/nolwag/Node/toggl/node_modules/mongodb/lib/utils.js:532:9
at /home/nolwag/Node/toggl/node_modules/mongodb/lib/mongo_client.js:130:17
at connectCallback (/home/nolwag/Node/toggl/node_modules/mongodb/lib/operations/connect.js:38:9)
at /home/nolwag/Node/toggl/node_modules/mongodb/lib/operations/connect.js:89:9
at /home/nolwag/Node/toggl/node_modules/mongodb/lib/sdam/topology.js:231:25
at /home/nolwag/Node/toggl/node_modules/mongodb/lib/cmap/connection_pool.js:272:25
at handleOperationResult (/home/nolwag/Node/toggl/node_modules/mongodb/lib/sdam/server.js:363:9)
https://docs.mongodb.com/manual/crud/ - 我正在复制文档,似乎只是这个发现不起作用。如果我将 .count() 附加到它的末尾,它会正常返回。
【问题讨论】:
标签: node.js mongodb mongodb-query