1

Is it possible to return empty objects if there is no records in db?

db.query(' SELECT * from users where type = $1 and category = $2 limit 8', [admin, filltime], function(err, result) {
            if (err) return next(err);
            return res.status(200).send({
                status: "Successfully displaying",
                data: result.rows
            })
        })

i have only 2 records in PostgreSQL db for this type and category but i requested for 8 records. Am expecting that 2 records along with 6 empty object so how can i achieve? is there any query for that ? please guide me

expected o/p:

[{
record 1
},
{
record 2
},
{},{},{},{},{},{}]
  • Why are you expecting a JSON response using SELECT *? – McNets Jan 9 '18 at 10:58
  • @McNets please check now i have updated my quire along with code, m returning json response – Schüler Jan 9 '18 at 11:03
  • Why do you expect more items than there are? You should fill the remaining 6 in the application code, not on the DB side. Not to mention it is impossible to achieve this simply using LIMIT. – dezso Jan 9 '18 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.