【问题标题】:Argument of type 'match' is not assignable to parameter of type any[] - nodejs / mongoose'match' 类型的参数不可分配给 any[] 类型的参数 - nodejs / mongoose
【发布时间】:2020-07-27 00:46:20
【问题描述】:

我的查询在 RoboMongo 中运行良好,但是当我在我的 nodeJs 环境中构建相同的查询时,我遇到了异常。

下面是我的代码

存储库函数

public find = async (account: any): Promise<any> => {
  return PromoTypes.aggregate(
    {
      $match: { account: { $in: account } }
    },
    {
      $group: {
        _id: '$account',
        data: {
          $push: { _id: '$_id', description: '$description', iteration: '$iteration' }
        }
      }
    }
  );
};

调用上述 repo 的服务文件。功能

AccountService.ts

public accountService = async (accountid: string): Promise<any> => {
 //accounttypes is an array constructed to pass to the repo, it has more business logic about construction which I have not included in this code. But the object would be as below

 accounttypes = ["first,second"]
 let accountDetails = await AccountRepo.find(accounttypes);
}

以下是我遇到的错误。

 Argument of type '{ $match: { account: { $in: any; }; }; }' is not assignable to parameter of type 'any[]'.   Object literal may only specify known properties, and '$match' does not exist in type 'any[]'.ts(2345)

我尝试了什么

我尝试将承诺类型更改为以下类型

 1. public accountService = async (accountid: string): Promise<any[]> =>{
 2. public accountService = async (accountid: string): Promise<Array<Object>> => {
 3. public accountService = async (accountid: string): Promise<Object> => {

以上方法都不能解决问题。

【问题讨论】:

标签: node.js mongodb typescript mongoose types


【解决方案1】:

您需要将数组传递给aggregate 管道

public find = async (account: any): Promise<any> => {
  return PromoTypes.aggregate([
    {
      $match: { account: { $in: account } }
    },
    {
      $group: {
        _id: '$account',
        data: {
          $push: { _id: '$_id', description: '$description', iteration: '$iteration' }
        }
      }
    }
  ]);
};

【讨论】:

    猜你喜欢
    • 2020-03-19
    • 2019-09-06
    • 2021-12-05
    • 2018-03-19
    • 2019-11-02
    • 2020-12-17
    • 1970-01-01
    • 1970-01-01
    • 2019-01-09
    相关资源
    最近更新 更多