2012-07-23 18 views
5

Voglio cercare nel mio repository utenti con una stringa di query.Expressjs: Cerca query api

Ciò dovrebbe restituire tutti gli utenti con un nome utente simile "kyogron" e simili e-mail "kyogron @ gmail"

GET localhost:3000/users?username=kyogron&[email protected] 

Ciò dovrebbe restituire tutti gli utenti:

GET localhost:3000/users 

ho già riuscito a gestire il parametri di instradamento ma sono bloccato a ottimizzarlo:

app.get('/users', function(req, res) { 
    // creates a mongoosejs query object 
    var query = User.find({}); 

    // to understand how expressjs handles queries: 
    // ?username=kyogron&[email protected] 
    // { username: "kyogron", email: "[email protected]" } 
    //console.log(req.query); 

    // this was one idea of optimizing the search query parameters 
    // this doesn't work don't know why I always get an array of ALL users 
    // even the key and value is right 
    Object.keys(req.query).forEach(function(key) { 
     query.select(key, req.query[key]); 
    }); 

    // this was the way I was first handling the parameters, this works !! 
    //if (req.query.username) query.where('username', req.query.username); 
    //if (req.query.email) query.where('email', req.query.email); 

    // the rest of the query 
    query.select('username', 'email'); 
    query.exec(function(err, users) { 
     if (err) throw err; 
     res.json(users); 
    }); 

}); 

Questi sono i p roblems con cui sto combattendo:

  1. Perché non funziona l'iterazione dell'oggetto req.query?
  2. Come faccio a dire mangusta per utilizzare un carattere jolly (ad esempio * kyo)

Sarebbe bello se qualcuno mi potrebbe aiutare :)

saluti

EDIT:

Il secondo problema sarebbe risolvibile con $ dove:

if (req.query.username) { 
     query.$where(function() { 
      return this.username === req.query.username; // here we need a regex check 
     }); 
    } 

Thos non funziona ... Qualcuno potrebbe darmi un suggerimento?

EDIT2:

Non ha gestito nulla con $ dove ... però io ora trovato

query.where('username').regex(); 

non mi resta che cercare un regex ricerca che cerca parole simili

Edit3:

ho trovato questa discussione: 01.238.364,359 milaChiedo al gruppo mongoosejs come avrei potuto farlo con mangusta

edit4:

if (req.query.username) { 
      query.where('username').regex(new RegExp("\/"+req.query.username+"\/")); 
} 

ho quasi preso. Basta per risolvere questo stupido regex ...

risposta

1
app.get('/users', function(req, res) { 
    var query = User.find({}); 

    Object.keys(req.query).forEach(function(key) { 
     query.where(key).regex(new RegExp(req.query[key])); 
    }); 

    /* 
    if (req.query.username) { 
     query.where('username').regex(new RegExp(req.query.username)); 
    } 
    if (req.query.email) { 
     query.where('email').regex(new RegExp(req.query.email)); 
    }*/ 

    query.select('username', 'email'); 
    query.exec(function(err, users) { 
     if (err) throw err; 
     res.json(users); 
    }); 

}); 

Il primo non ha funzionato perché ho avuto un errore di battitura (.select() non .dove()). Il secondo è stato trovato in un thread aggiuntivo

Sono ancora un po 'incerto sull'approccio scelto.

Iterazione req.query permetterebbe di rendere il codice riutilizzabile (forse come precondizione di routing parametro di funzione), ma è molto sensibili per errori