如何在router.get中创建空数组?

时间:2017-10-02 22:49:07

标签: json express

我试图做出类似的事情。

{
    suggestions: [{
        "nameClient": "John",
        "nameFantasyClient": "smachs"
    }, {
        "nameClient": "José",
        "nameFantasyClient": "apoch"
    }]
}

无法找到有关如何制作空数组的任何信息。这是我收到的api / product / sell / name-of-client-search。

[
    {
        "nameClient": "John",
        "nameFantasyClient": "smachs"
    },
    {
        "nameClient": "José",
        "nameFantasyClient": "apoch"
    }
]

这是我的router.get负责从数据库中提取数据并将其发送到前端。

router.get('/product/sell/name-of-client-search', function (req, res) {
  connection.query('SELECT nameClient,nameFantasyClient FROM new_client',
    function (err, nameOfClientToSell, fields) {
      if (err) {
        throw err;
      }
      res.send(nameOfClientToSell);
    }
  );
});

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

你可以动态构建json。

router.get('/product/sell/name-of-client-search', function (req, res) {
  connection.query('SELECT nameClient,nameFantasyClient FROM new_client',
    function (err, nameOfClientToSell, fields) {
      if (err) {
        throw err;
      }
      res.send({ suggestions: nameOfClientToSell });
    }
  );
});