Gabriel Correa Gabriel Correa - 11 months ago 131
JSON Question

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.

[
{
"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('/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!

Answer Source

You can build the json on the fly.

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 });
    }
  );
});
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download