Search code examples
postgresqlsails.jspgadmin

Valid Postgres query alway return undefined


I am using sails to build an API, one of my function needs to get columns from my PGSQL database. My SQL request is working when I test it in PGadmin, it looks like this :

SELECT "user".id, "user".picture, "user".first_name, "trophe".trophe FROM "user" INNER JOIN "trophe" ON "trophe".user  = "user".id WHERE "trophe".foot = 1

When I transcript this request in my sails API, it looks like this :

  getHommeAndChevre: function (req, res) {
    console.log(req.param("id"));
    Trophe.query("SELECT 'user'.id, 'user'.picture, 'user'.first_name, 'trophe'.trophe FROM 'user' INNER JOIN 'trophe' ON 'trophe'.user = 'user'.id WHERE 'trophe'.foot ="+req.param('id'), function(err,trophes){
      if(!trophes) {return res.status(400).end();}
      if(trophes){
        _each(trophes, function(trophe){
          if (trophe.trophe == 0) {var chevre = trophe};
          if (trophe.trophe == 1) { var homme = trophe};
        })
       return res.status(200).json({chevre: chevre, homme: homme})
      }
    });
  }

However whatever I do the Trophe.query always returned undefined. What could be wrong here ?


Solution

  • Use double quotes for identifiers

    Trophe.query('SELECT "user".id, "user".picture, "user".first_name, ...