首页 > 解决方案 > 与 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();
  };
}

标签: node.jsmongodbazuremongooseazure-functions

解决方案


find()返回一个游标,它有一个select方法。 findOne()检索单个文档并在未提供回调时返回一个承诺。

如果您只想获取“notificationBy”字段,请尝试将fields选项传递给 findOne。


推荐阅读