最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

如何在 node.js 中使用带有模型接口的打字稿填充 mongdb 包

网站源码admin25浏览0评论

如何在 node.js 中使用带有模型接口的打字稿填充 mongdb 包

如何在 node.js 中使用带有模型接口的打字稿填充 mongdb 包

我正在使用带有此接口(模型)的打字稿,名为 invoice.ts:

import { ObjectId } from "mongodb";

interface Invoice {
 _id: ObjectId;
 accountId: ObjectId;
 number: string;
 invoiceDate: Date;
 amount: number;
 paymentDate: Date;
 expectedPaymentDate: Date;
 updatedAt: Date;
 createdAt: Date;
 paymentAt: Date;
 repaymentAt: Date;
 reservation: object;
 totalCosts: number;
}

export { Invoice };

我想检索所有发票,其名称为对象的 accountId 以及发票的编号 + id + createdAt

在 service-loans.ts

我试过这个:

  import { Invoice } from "../models/invoice";
  import { MongoClient } from "mongodb";
  import logger from "../lib/logging/logger";

  const url: string = process.env["MONGODB_URL"] || "";
  const dbName: string = process.env["MONGODB_DATABASE"] || "";
  const client = new MongoClient(url);

  const getInvoices = async () => {
   try {
    await client.connect();
    const db = client.db(dbName);
    const invoiceCollection = db.collection<Invoice>("invoice");
    const invoices = await invoiceCollection
     .find(
      {},
       {
        projection: {
         accountId: 1,
         number: 1,
         id: 1,
         createdAt: 1,
        },
       },
      )
      .populate([{ path: "accountId", select: "name" }])
      .lean();
   }
   catch (error) {
    logger.error(error);
   }

 };
 export default { getInvoices };

它通过 populate 提示错误:类型上不存在属性“populate”。 TS(2339)。我需要路径名称“accountId”

有谁知道如何使查询正确吗?

回答如下:
发布评论

评论列表(0)

  1. 暂无评论