Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/414.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 聚合不是一个函数-Mongoose Nodejs_Javascript_Node.js_Mongodb_Mongoose - Fatal编程技术网

Javascript 聚合不是一个函数-Mongoose Nodejs

Javascript 聚合不是一个函数-Mongoose Nodejs,javascript,node.js,mongodb,mongoose,Javascript,Node.js,Mongodb,Mongoose,有人能帮我解决这个问题吗?这是我从mongoose获得的聚合代码: export class GetVehiclesbyKotaCommandHandler { constructor(namaKota) { return new Promise((resolve, reject) => { VehiclesDB.find().populate({ path: 'mitraId', model: 'RentalDB',

有人能帮我解决这个问题吗?这是我从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
完成,我感谢Hana:) 我改变了我的mitraId类型ObjectId 密特拉德:{ 类型:Schema.Types.ObjectId, 必填项:true
},

您可以在聚合语句中使用
$lookup
,而不是在此处使用
find
,以及
populate

像这样:

VehiclesDB.aggregate([
    {
      $lookup: {
        from: 'RentalDB',
        localField: 'mitraId',
        foreignField: '_id',
        as: 'mitra'
      }
    }, {
      $unwind: "$mitra"
    }, {
      $match: {
        "mitra.namaKota": namaKota
      }
    }
  ])

我希望这会有所帮助。

尽量避免在此处查找、填充、精简功能,如下所示

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));
        });
    });
}}

我试图改进你的应答代码,这是成功的,而不是错误的,但我的应答中没有任何数据,我的exec===>>>.exec((err,dataVehicles)=>{如果(err)拒绝(newnotfoundexception('notfind any Vehicles with id'+namaKota');解析(dataVehicles);});您好@GalangArbiS,您能为您的问题添加一些数据示例吗?:)