Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

228
Views
el parámetro de consulta no está en la sintaxis correcta

Estoy tratando de filtrar una colección por nombre (u otros campos) al verificar los parámetros de URL y su valor, por ejemplo

 http://localhost:3000/patient?filter=name:jack

Tengo un método para recuperar el cochecito de URL y convertirlo en un objeto json:

 const filter = handleQueryFilter(req.query.filter) const handleQueryFilter = (query) => { try{ // convert the string to look like json object // example id: -1, name: 1 to { id: "-1"}, {name: "1" } const toJSONString = ("{" + query ).replace(/(\w*[^:].$)/g, (matched => { return '"' + matched.substring(0, matched.length ) + '"}' ; })); return JSON.parse(toJSONString); }catch(err){ return JSON.parse("{}"); // parse empty json if the clients input wrong query format } }

Lo que alguna vez devolvió el 'handleQueryFilter' se pasa al método 'find' para obtener el resultado de la base de datos

 let patients = await Patient.find(filter);

Sin embargo, handleQueryFilter siempre devuelve un objeto vacío (la parte de captura anterior), ¿qué estoy haciendo mal?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Primero: haga que express pase req.body automáticamente usando

 app.use(express.json())

Edite su URL para que sea: http://localhost:3000/patient?name=jack

about 4 years ago · Juan Pablo Isaza Report

0

Su operación de replace no hace lo que se supone que debe hacer:

 "{id: -1, name: 1".replace(/(\w*[^:].$)/g, (matched => { return '"' + matched.substring(0, matched.length ) + '"}' }))

devuelve '{id: -1, name:" 1"}' , que no es JSON válido, por lo tanto, JSON.parse da un error.

Prueba algo como

 Object.fromEntries("id: -1, name: 1".split(/,\s*/).map(_=>_.split(/:\s*/)))

que devuelve {id: '-1', name: '1'} sin necesidad de JSON.parse .

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!