1

I have an application where I want to list a bunch of Artists by letter. So for example if I enter the letter "L" - the application should return all artists from my DB with the letter "L". I'm pretty new to NodeJS, so maybe someone can help me out?

my routes.js:

server.get('/i/artists/:letter', myartists.getArtistByLetter);

and my artists.js:

exports.getArtistByLetter = function(req, res, next){

   var where;

   if(req.query == '0-9') // not sure about req.query, probably wrong???
   {
      where = ["formated_name RLIKE '^[0-9#]' AND artist_parent_id = 0 "];
   }
   else  
   {
      where = ["formated_name LIKE '"+req.query+"%' AND artist_parent_id = 0"];
   }    

   db.artist.findAll({ 
      where: where, 
      attributes: ['artist_id', ['formated_name', 'name'], 'uuid', 'slug']
    }, {raw: true})
    .success(function(artist){
       res.json({data: artist});
   });  

}

After entering http://localhost:2100/i/artists/l this returns {"data":[]}...

so, any suggestions? Thanks...

ST80
  • 3,565
  • 16
  • 64
  • 124

1 Answers1

1

To acces letter parameter you have to write req.params.letter instead of req.query.

Take a look at the answer of how to hande a get request with nodejs.

On this url: http://localhost:2100/i/artists/l the req.params.letter wil have the value l.

Community
  • 1
  • 1
adricadar
  • 9,971
  • 5
  • 33
  • 46