【发布时间】:2021-05-20 14:27:06
【问题描述】:
当我在搜索框中输入 phone 时,我在数据库中找到并获取了所有带有 phone 字样的类别。然后我想通过将这个类别的_id号与产品的类别id号匹配来找到产品。但我无法收集在单个数组中找到的产品。这就是为什么我不能将它们全部打印在屏幕上的原因。由于在数组中创建了两个不同的数组,因此它会打印第一个数组中的产品,但不会传递给第二个数组。
从图片中可以看出,我无法打印它,因为第三个产品在另一个数组中。
function escapeRegex(text) {
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
};
let model = [];
const searchRegex = new RegExp(escapeRegex(req.query.search), 'gi');
SubSubCategory.find({ "name": searchRegex})
.then(subsubcategoriesProduct => {
subsubcategoriesProduct.forEach(p => {
Product.find({ categories: p._id })
.then(finalProduct => {
model.push(finalProduct);
res.render('shop/products', {
title: 'Tüm Ürünler',
products: model,
path: '/products',
searchRegex: searchRegex
});
...
【问题讨论】:
标签: javascript node.js arrays mongodb backend