node.js - 与 Azure 函数一起使用时,Mongoose .select 不是函数
问题描述
我正在尝试将 mongoose .select 运算符与我的 azure 函数一起使用,但它一直说它TypeError: db.collection(...).findOne(...).select is not a function at db.collection.find.toArray
在控制台中返回用户的数据,但没有使用.select
Why is that?
var MongoClient = require('mongodb').MongoClient;
var Post = require('./model/post');
var mongoose = require('mongoose');
module.exports = async function (context, req) {
let currentPage = 1;
MongoClient.connect(process.env.CosmosDBConnectionString, async (err, client) => {
let send = response(client, context);
if (err) send(500, err.message);
let db = client.db(process.env.dbName);
await db.collection('listings').find(
{
$and: [
{ winnerHasBeenNotified: false },
{ auctionEndDateTime: { $lte: Date.now().toString() } }
]
}
)
.toArray((err, result) => {
console.log("result");
console.log(result);
if (result) {
for (let i = 0; i < result.length; i++) {
db.collection('users').findOne(
{
_id: mongoose.Types.ObjectId(result[i].bidderId)
}
).select("notificationBy").toArray((err, result) => {
console.log("USER RESULT!");
console.log(result);
});
}
}
if (err) send(500, err.message);
send(200, JSON.parse(JSON.stringify(result)));
});
});
};
function response(client, context) {
return function (status, body) {
context.res = {
status: status,
body: body
};
client.close();
context.done();
};
}
解决方案
find()
返回一个游标,它有一个select
方法。 findOne()检索单个文档并在未提供回调时返回一个承诺。
如果您只想获取“notificationBy”字段,请尝试将fields
选项传递给 findOne。
推荐阅读
- python - 带有文件大小的 Python TQDM 缩放进度条
- docker - Minio docker容器在重新创建后没有文件权限
- spring - 如何自定义 SpringBoot 执行器输出格式?
- node.js - 使用 ffmpeg 从 mediasoup 路由器记录 RTP 时出现问题
- typescript - 使用 next.js,如何限制对 API 路由的请求仅来自网页?
- python-3.x - 如何从列表中的嵌套字典对象中提取选定的键和值?
- python - 如果数据不完整,如何加入绘图点?
- sql - 如何将以下格式转换为 unix 时间戳?
- solidity - 如何设置 tx ETH 数量?
- javascript - 阻止字母移动