Node.js nodejsmongdb使用async/await查询数据
我使用节点模块Node.js nodejsmongdb使用async/await查询数据,node.js,mongodb,asynchronous,async-await,Node.js,Mongodb,Asynchronous,Async Await,我使用节点模块mongodb v3.2.7,并使用express实现我的api 我使用回调。而且,它工作得很好 函数findItem const findItem = function (id, callback) { if (db) { const collection = db.collection('transaction') collection.find({ user_id: id}) .project({ ready: 1, online: 1, st
mongodb v3.2.7
,并使用express
实现我的api
我使用回调
。而且,它工作得很好
函数findItem
const findItem = function (id, callback) {
if (db) {
const collection = db.collection('transaction')
collection.find({ user_id: id})
.project({ ready: 1, online: 1, status: 1})
.toArray(function (err, docs) {
if (err) {
callback(err)
} else {
callback(null, docs)
}
})
}
}
我的api
app.get('/', (req, res) => {
try {
if (req.query.user_id) {
const contentType = 'application/json'
res.set({ 'Access-Control-Allow-Origin': '*',
'Content-Type': contentType })
const user_id = parseInt(req.query.user_id, 0)
findItem(user_id , function (err, docs) {
if (err) {
res.status(500).end()
return res.send(err)
}
const data = processData(docs)
console.log('data', data)
return res.send(data)
})
} else {
res.send()
}
} catch (ex) {
console.log(ex)
}
})
然后,我尝试使用
async/await
而不是callback
。但是,它不起作用。我犯了什么错误
函数findItemAsyncAwait
const findItemAsyncAwait = async function (id) {
if (db) {
const collection = db.collection('transaction')
var docs = await collection.find({ user_id: id })
.project({ ready: 1, online: 1, status: 1})
.toArray()
return docs
}
}
我更改的api
app.get('/', (req, res) => {
try {
if (req.query.user_id) {
const contentType = 'application/json'
res.set({ 'Access-Control-Allow-Origin': '*',
'Content-Type': contentType })
const user_id = parseInt(req.query.user_id, 0)
const promiseDocs = findItemAsyncAwait(user_id)
promiseDocs.then((docs) => {
console.log('docs', docs) // <= docs [], docs is an empty array without any document
const data = processData(docs)
console.log('data', data)
return res.send(data)
}).catch((err) => {
res.status(500).end()
return res.send(err)
})
} else {
res.send()
}
} catch (ex) {
console.log(ex)
}
})
app.get('/',(req,res)=>{
试一试{
if(请求查询用户id){
const contentType='application/json'
res.set({'Access Control Allow Origin':'*',
“内容类型”:内容类型})
const user_id=parseInt(req.query.user_id,0)
const promiseDocs=findItemAsyncAwait(用户id)
承诺文件。然后((文件)=>{
console.log('docs',docs)//{
资源状态(500).结束()
返回res.send(err)
})
}否则{
res.send()
}
}捕获(ex){
控制台日志(ex)
}
})
我仍然无法解决这个问题。有人能帮忙吗?您必须等待收集
const findItemAsyncAwait = async function (id) {
if (db) {
return await db.collection('transaction')
.find({ user_id: id })
.project({ ready: 1, online: 1, status: 1})
.toArray()
}
}
这不仅不起作用,而且还会引起
eslint
警告对返回值的等待的冗余使用。docs
仍然是一个空数组。eslint警告在大多数情况下是正确的-它应该是一个直接的返回db.collection()
并且可以在不使用异步的情况下声明函数。对于功能而言,在不使用回调的情况下使用的db.collection()
应该像承诺一样。使用回调方法,它工作得很好。因此,我认为第行const collection=db.collection('transaction')没有问题
和collection.find({user\u id:id})..
尝试使用console.log(db.collection('transaction'))
-如果这是一个承诺,你必须执行const collection=wait db.collection('transaction')
它是一个对象,而不是承诺。