我的 api 代码从 mongodb 检索空数据数组,而代码在 mongodb 操场上运行良好 [英] My api code retrieves an empty data array from mongodb while the code works well on mongodb playground

查看:26
本文介绍了我的 api 代码从 mongodb 检索空数据数组,而代码在 mongodb 操场上运行良好的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 mongodb 数据库包含一组用户,每个用户都有一系列商店,每个商店都有一系列产品.这是我的收藏结构的简化版本:

<预><代码>[{_id":60e66b70da2439232e330415",名称":用户 1 名称",商店":[{_id":60e7c9e2be0d8f03544a03b8","shopName": "User1 Shop1 Name",产品":[{_id":60e9e9e8105d6021a2e91535",title":User1 Shop1 Product1 Title"},{_id":60e9f4a0105d6021a2e91536",title":User1 Shop1 Product2 Title"}]},{_id":60e8e8c00f3986577cb968c9",商店名称":用户 1 商店 2 名称",产品":[{_id":60e9f4fe105d6021a2e91537",title":User1 Shop2 Product1 Title"},{_id":60e9f515105d6021a2e91538",title":User1 Shop2 Product2 Title"}]}]},{_id":60e66b93da2439232e330416",名称":用户 2 名称",商店":[{_id":60e69698e76cad44e49e1fc8",shopName":User2 Shop1 名称",产品":[{_id":60e9f588105d6021a2e91539",title":User2 Shop1 Product1 Title"},{_id":60e9f59c105d6021a2e9153a",title":User2 Shop1 Product2 Title"}]},{_id":60e902b441e9df63c7fbcb49",shopName":User2 Shop2 名称",产品":[{_id":60e9f5c9105d6021a2e9153b",title":User2 Shop2 Product1 Title"},{_id":60e9f5de105d6021a2e9153c",title":User2 Shop2 Product2 Title"}]}]}]

我有一个 api 端点,如 .../api/products/60e9f5de105d6021a2e9153c.此端点包含一个参数,即 productId.我有以下两个代码可以从我的 mongodb 集合中检索产品数据,但检索到的数组为空.

我的端点代码:

app.get("/api/products/:productId", (req,res)=>{让 productId = req.params.productId;myData.getProductByProductId(productId).then(products => res.json(products)).catch(err => res.json({message": err}));});

我的数据服务代码:

getProductByProductId: 函数 (productId) {返回新的承诺((解决,拒绝)=> {用户.聚合([{$匹配:{shops.products._id":productId}},{$unwind":$shops"},{$unwind":$shops.products"},{$匹配:{shops.products._id":productId}},{$项目:{_id":$shops.products._id",标题":$shops.products.title"}}]).exec().then(products => {解决(产品)}).catch(err => {拒绝(错误);});});}

聚合代码在 这个游乐场 中运行良好,但在我的网络应用程序代码中检索到一个空数组.

解决方案

原来 ObjectId 的转换似乎是问题所在.我们需要使用 mongoose.Types.ObjectId

API 代码如下所示

getProductByProductId: 函数 (productId) {返回新的承诺((解决,拒绝)=> {用户.聚合([{$匹配:{shops.products._id":mongoose.Types.ObjectId(productId)}},{$unwind":$shops"},{$unwind":$shops.products"},{$匹配:{shops.products._id":mongoose.Types.ObjectId(productId)}},{$项目:{_id":$shops.products._id",标题":$shops.products.title"}}]).then(产品=> {解决(产品)}).catch(err => {拒绝(错误);});});}

My mongodb database includes a collection of users and each user has an array of shops and each shop has an array of products. Here is a simplified version of my collection structure:

[
    {
        "_id": "60e66b70da2439232e330415",
        "name": "User1 Name",
        "shops": [
            {
                "_id": "60e7c9e2be0d8f03544a03b8",
                "shopName": "User1 Shop1 Name",
                "products": [
                    {
                        "_id": "60e9e9e8105d6021a2e91535",
                        "title": "User1 Shop1 Product1 Title"
                    },
                    {
                        "_id": "60e9f4a0105d6021a2e91536",
                        "title": "User1 Shop1 Product2 Title"
                    }
                ]
            },
            {
                "_id": "60e8e8c00f3986577cb968c9",
                "shopName": "User1 Shop2 Name",
                "products": [
                    {
                        "_id": "60e9f4fe105d6021a2e91537",
                        "title": "User1 Shop2 Product1 Title"
                    },
                    {
                        "_id": "60e9f515105d6021a2e91538",
                        "title": "User1 Shop2 Product2 Title"
                    }
                ]
            }
        ]
    },
    {
        "_id": "60e66b93da2439232e330416",
        "name": "User2 Name",
        "shops": [
            {
                "_id": "60e69698e76cad44e49e1fc8",
                "shopName": "User2 Shop1 Name",
                "products": [
                    {
                        "_id": "60e9f588105d6021a2e91539",
                        "title": "User2 Shop1 Product1 Title"
                    },
                    {
                        "_id": "60e9f59c105d6021a2e9153a",
                        "title": "User2 Shop1 Product2 Title"
                    }
                ]
            },
            {
                "_id": "60e902b441e9df63c7fbcb49",
                "shopName": "User2 Shop2 Name",
                "products": [
                    {
                        "_id": "60e9f5c9105d6021a2e9153b",
                        "title": "User2 Shop2 Product1 Title"
                    },
                    {
                        "_id": "60e9f5de105d6021a2e9153c",
                        "title": "User2 Shop2 Product2 Title"
                    }
                ]
            }
        ]
    }
]

I have an api endpoint like .../api/products/60e9f5de105d6021a2e9153c. This endpoint includes a parameter which is a productId. I have the following two codes to retrieve the product data from my mongodb collection, but the retrieved array is empty.

My endpoint code:

app.get("/api/products/:productId", (req,res)=>{
    let productId = req.params.productId;
    myData.getProductByProductId(productId)
    .then(products => res.json(products))
    .catch(err => res.json({"message": err}));
});

My DataService code:

getProductByProductId: function (productId) {
            return new Promise((resolve, reject) => {
                User.aggregate([
                    {
                      $match: {
                        "shops.products._id": productId
                      }
                    },
                    {
                      "$unwind": "$shops"
                    },
                    {
                      "$unwind": "$shops.products"
                    },
                    {
                      $match: {
                        "shops.products._id": productId
                      }
                    },
                    {
                      $project: {
                        "_id": "$shops.products._id",
                        "title": "$shops.products.title"
                      }
                    }
                  ])
                .exec().then(products => {
                    resolve(products)
                }).catch(err => {
                    reject(err);
                });
            });
        }

The aggregate code works well in this playground but in my web app code retrieves an empty array.

解决方案

Turns out the casting of the ObjectId seemed to be the issue. We need to use mongoose.Types.ObjectId

The API code will looks like this

getProductByProductId: function (productId) {
return new Promise((resolve, reject) => {
  User.aggregate([
  {
  $match: {
    "shops.products._id": mongoose.Types.ObjectId(productId)
  }
  },
  {
    "$unwind": "$shops"
  },
  {
    "$unwind": "$shops.products"
  },
  {
    $match: {
    "shops.products._id": mongoose.Types.ObjectId(productId)
  }
  },
  {
  $project: {
  "_id": "$shops.products._id",
  "title": "$shops.products.title"
  }
}
])
.then(products => {
resolve(products)
}).catch(err => {
reject(err);
});
});
}

这篇关于我的 api 代码从 mongodb 检索空数据数组,而代码在 mongodb 操场上运行良好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆