简体   繁体   English

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

[英]How to create empty array in router.get?

I'm trying to make something like that. 我正在尝试做类似的事情。

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

Can not find anything about how to make empty array. 找不到有关如何制作空数组的任何信息。 This is a return i am receiving to my api /product/sell/name-of-client-search. 这是我收到的对我的api / product / sell / name-of-client-search的回报。

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

This my router.get responsible for extracting data from the database and sending it to front-end. 我的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);
    }
  );
});

Thanks for helping! 感谢您的帮助!

You can build the json on the fly. 您可以动态构建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 });
    }
  );
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM