【问题标题】:How do I handle a MySQL query with multiple parameters whilst using Angular / Express如何在使用 Angular / Express 时处理具有多个参数的 MySQL 查询
【发布时间】:2016-04-09 16:59:51
【问题描述】:

我正在使用 AngularJS 和 NodeJS/Express 设计一个二手车网站。数据库是 MySQL(所以不是一个 MEAN 堆栈)。

我对 Angular 非常熟悉,但这是我第一次使用 Express(过去我使用过 JAX-RS)。

我只发送一两个参数没有问题,例如

app.get('/api/images/:vehicleId', function (request, response) {
    images.getThumbnailImage(request, response, connection, request.params.vehicleId);
});

但我不确定如何使用多个参数前进,其中一些是可选的。

客户端是使用 TypeScript 构建的。表单数据是通过 TypeScript 对象收集的,但我不确定发送多个参数的最佳做法是什么。

我想到的选项是:

1)。发送对象,然后使用它来构建查询:

return this.$http({
    method: 'POST',
    url: this.API_ADDRESS + 'vehicles/',
    data: vehicleSearchModel,
    headers: {
      'Content-Type': 'application/json'
    }
  }).then((response: any) => {
    return response.data;
  });

并使用body-parser从body中检索它,即

request.body.data (or something similar)

2)。在 url 中发送多个参数,即

app.get('/api/vehicles/:make/:model/:bodyStyle/:fuelType/:transmission/:minPrice/:maxPrice/:minYear/:maxYear/:counties', function (request, response) {
    //Do something with request.params!!!
});

请指教。

【问题讨论】:

    标签: mysql angularjs node.js express


    【解决方案1】:

    首先,您应该如下发出http post请求。

    var data = {
        make : make,
        model : model,
        fuelType : fueltype,
    ....
    }
    return this.$http({
        method: 'POST',
        url: this.API_ADDRESS + 'vehicles/',
        data: vehicleSearchModel,
        headers: {
          'Content-Type': 'application/json'
        }
      }).then((response: any) => {
        return response.data;
      });
    

    然后,您可以接收多个参数,如下所示。

    app.post('/api/vehicles/', function(req, res){
      var make = req.body.make;
      var model= req.body.model;
      var bodyStyle= req.body.bodyStyle;
      var fuelType= req.body.fuelType;
      var transmission= req.body.transmission;
    .....
    });
    

    【讨论】:

    • 好的。谢谢。这是我将遵循的路线。
    猜你喜欢
    • 1970-01-01
    • 2021-11-28
    • 1970-01-01
    • 1970-01-01
    • 2016-05-31
    • 1970-01-01
    • 1970-01-01
    • 2017-03-24
    • 2015-06-22
    相关资源
    最近更新 更多