类型'match'的参数不能分配给any []类型的参数-nodejs/mongoose [英] Argument of type 'match' is not assignable to parameter of type any[] - nodejs / mongoose
本文介绍了类型'match'的参数不能分配给any []类型的参数-nodejs/mongoose的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的查询在RoboMongo中运行良好,但是当我在nodeJs环境中构造相同的查询时,就会出现异常.
My query is running fine in RoboMongo, but when I construct the same in my nodeJs environment I'm getting the exception.
下面是我的代码
存储库功能
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类型更改为以下类型
I tried changing the promise type to below types
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> => {
以上方法均无法解决该问题.
None of the above approaches work for fixing the issue.
推荐答案
您需要将数组传递给aggregate
管道
you need to pass an array to the aggregate
pipeline
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' }
}
}
}
]);
};
这篇关于类型'match'的参数不能分配给any []类型的参数-nodejs/mongoose的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文