gpt4 book ai didi

javascript - 聚合不是函数 - Mongoose Nodejs

转载 作者:行者123 更新时间:2023-12-03 03:21:35 26 4
gpt4 key购买 nike

有人可以帮我解决这个问题吗,这是我从 Mongoose 聚合的代码:

export class GetVehiclesbyKotaCommandHandler {
constructor(namaKota) {
return new Promise((resolve, reject) => {
VehiclesDB.find().populate({
path: 'mitraId',
model: 'RentalDB',
select: 'namaKota'
}).aggregate([
{
$match : {
namaKota:namaKota
}
}
]).lean().then((dataVehicles)=>{
if(dataVehicles !== null){
resolve(dataVehicles);
} else {
reject (new NotFoundException('Couldn\'t find any Vehicles with namaKota' + namaKota));
}
}).catch((errDataVehicles)=>{
reject(new CanNotGetVehiclesException(errDataVehicles.message));
});
});
}}

我在控制台上收到这样的错误:

TypeError: _VehiclesDB2.default.find(...).populate(...).aggregate is not a function

完成,我感谢哈娜:)我更改了 mitraId 类型 ObjectId 米特拉ID:{ 类型:Schema.Types.ObjectId, 必填:真实 },

最佳答案

尽量避免此处的查找、填充、精益功能,并按如下方式操作

export class GetVehiclesbyKotaCommandHandler {
constructor(namaKota) {
return new Promise((resolve, reject) => {
VehiclesDB.aggregate([
{
$lookup: {
from: 'RentalDB',
localField: 'mitraId',
foreignField: '_id',
as: 'mitra'
}
}, {
$unwind: "$mitra"
}, {
$match: {
"mitra.namaKota": namaKota
}
}
]).then((dataVehicles)=>{
if(dataVehicles !== null){
resolve(dataVehicles);
} else {
reject (new NotFoundException('Couldn\'t find any Vehicles with namaKota' + namaKota));
}
}).catch((errDataVehicles)=>{
reject(new CanNotGetVehiclesException(errDataVehicles.message));
});
});
}}

关于javascript - 聚合不是函数 - Mongoose Nodejs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46555413/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com