【问题标题】:I want to pass the _id as parameter in http get request in Angular我想在 Angular 的 http get 请求中将 _id 作为参数传递
【发布时间】:2018-09-26 10:21:05
【问题描述】:
  productSelect(_id){
    let headers = new Headers();
    headers.append('Content-Type', 'application/json');
    return this.http.get('http://localhost:3000/api/productSelect', { headers: headers, search: _id }).pipe(map(res => res.json()));
  }

index.js(节点)

router.get('/productSelect', (req, res, next) =>{
    let _id = req.query.search;
    console.log(_id);
    productSchema.aggregate([   
        { $lookup:
            {
                from: 'supplierschemas',
                localField: 'supplierId',
                foreignField: '_id',
                as: 'supplier'
            }
        },
        { $lookup:
            {
                from: 'brandschemas',
                localField: 'brandId',
                foreignField: '_id',
                as: 'brand'
            }
        },
        { $lookup:
            {
                from: 'categoryschemas',
                localField: 'categoryId',
                foreignField: '_id',
                as: 'category'
            }
        }

    ], (err, productSchema) =>{
        if(err) res.json(err);
        else res.json(productSchema);
    });
});

此代码返回“未知”。如何解决这个问题?以及如何添加传递参数到聚合?我想根据用户的选择修改选择标签选项

【问题讨论】:

    标签: node.js angular typescript


    【解决方案1】:

    看起来您需要req.query.search 而不是req.body.search。来自文档

    req.query

    路由中的查询字符串参数。如果没有查询字符串,它 是空对象,{}。此属性是包含每个属性的对象

    【讨论】:

    • 仍然返回未定义
    • 看起来您需要在get(..) 添加而不是搜索,而是像let params=new HttpParams(); params.set("search",_id); 一样添加邮件并将您的{header:header,search:_id} 替换为{header:header,params:params}
    • node.js接收到的值如何解析?
    • 在建议的解决方案中
    • @CharlesEdwinIson 您是否尝试在客户端添加 params,就像之前的评论一样?
    猜你喜欢
    • 2020-09-11
    • 2022-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多