'match'类型的参数不能分配给any []类型的参数[]-nodejs / mongoose

问题描述 投票:0回答:1

我的查询在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' }
        }
      }
    }
  );
};

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

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)

我尝试了什么

我尝试将Promise类型更改为以下类型

 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个回答
0
投票

您需要将数组传递到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' }
        }
      }
    }
  ]);
};
© www.soinside.com 2019 - 2024. All rights reserved.