簡體   English   中英

我的 api 代碼從 mongodb 檢索空數據數組,而代碼在 mongodb 操場上運行良好

[英]My api code retrieves an empty data array from mongodb while the code works well on mongodb playground

我的 mongodb 數據庫包含一組用戶,每個用戶都有一系列商店,每個商店都有一系列產品。 這是我的收藏結構的簡化版本:

[
    {
        "_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"
                    }
                ]
            }
        ]
    }
]

我有一個像.../api/products/60e9f5de105d6021a2e9153c這樣的 api 端點。 此端點包含一個參數,即 productId。 我有以下兩個代碼來從我的 mongodb 集合中檢索產品數據,但檢索到的數組為空。

我的端點代碼:

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

我的數據服務代碼:

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

聚合代碼在這個操場上運行良好,但在我的網絡應用程序代碼中檢索一個空數組。

結果證明 ObjectId 的轉換似乎是問題所在。 我們需要使用mongoose.Types.ObjectId

API 代碼如下所示

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM