$in 需要一个数组作为第二个参数,发现:缺失,此错误显示

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

我正在做电子商务网站,用户来将产品放入购物车。我想在购物车中显示产品,但显示错误

                getCartProducts: (userId) => {
    return new Promise(async (resolve, reject) => {
        let cartItems = await db.get().collection(collection.CART_COLLECTION).aggregate([
            {
                $match: { user: new ObjectId(userId) }
            },
            {
                $lookup: {
                    from: collection.PRODUCT_COLLECTION,
                    let: { prodList: '$products' },
                    pipeline: [
                        {
                            $match: {
                                $expr:{
                                    $in: ['$_id', "$$prodList"] 
                                }
                            }
                        
                        }
                    ],
                    as: 'cartItems'
                }
            },
            {
                $project: {
                    cartItems: 1
                }
            }
        ]).toArray();
        resolve(cartItems);
    });
}

这是我的代码,其中包含 $in 。但显示错误,请任何人都可以帮助我

node.js visual-studio-code visual-web-developer
1个回答
0
投票
pipeline: [
    {
        $match: {

            $expr: { $eq: [{ $type: "$$prodList" }, "array"] }
        }
    },
    {
        $match: {
            $expr: {
                $in: ['$_id', "$$prodList"]
            }
        }
    }
],

在管道中添加另一个

$match
将确保您将
$$prodList
作为数组。

© www.soinside.com 2019 - 2024. All rights reserved.