Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

247
Vistas
TypeError: no se puede crear la propiedad '_id' en la cadena

Estoy creando una API de descanso simple. Tengo un punto final para la publicación/datos para guardar datos en MongoDB desde una API externa.

Esto es lo que tengo hasta ahora:

 app.post('/data', (req, res) => { let url = 'https://api.themoviedb.org/3/movie/401478/reviews?api_key=4d9c9de3bdf0d3b6837c49c086e3b190'; request(url, function (error, response, body) { db.collection('data').insert(body, (err, result) => { if (err) { res.send({ 'error': 'An error has occured' }); } else { res.send(result.ops[0]); } }); }); });

cuando pruebo api en postman localhost:8000/data en la consola, aparece el error:

TypeError: no se puede crear la propiedad '_id' en la cadena

¿Qué estoy haciendo mal aquí?

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

body es la cadena JSON, para convertirla en un objeto JSON, use JSON.parse(body)

 app.post('/data', (req, res) => { let url = 'https://api.themoviedb.org/3/movie/401478/reviews?api_key=4d9c9de3bdf0d3b6837c49c086e3b190'; request(url, function (error, response, body) { // convert to JSON var bodyJson = JSON.parse(body) db.collection('data').insert(bodyJson, (err, result) => { if (err) { res.send({ 'error': 'An error has occured' }); } else { res.send(result.ops[0]); } }); }); });
over 4 years ago · Santiago Trujillo Denunciar

0

Según su pregunta anterior, veo que su cuerpo var es actualmente una cadena, por lo que debe cambiarlo.

 app.post('/data', (req, res) => { let url = 'https://api.themoviedb.org/3/movie/401478/reviews?api_key=4d9c9de3bdf0d3b6837c49c086e3b190'; request(url, function (error, response, body) { var insertObj = JSON.parse(body) db.collection('data').insert(insertObj, (err, result) => { if (err) { res.send({ 'error': 'An error has occured' }); } else { res.send(result.ops[0]); } }); }); });
over 4 years ago · Santiago Trujillo Denunciar

0

Como puede ver, "_id no puede ser una cadena"

cuando solicita la URL " https://api.themoviedb.org/3/movie/401478/reviews?api_key=4d9c9de3bdf0d3b6837c49c086e3b190 "

devolverá una cadena que se parece a JSON

 '{"id":401478,"page":1,"results":[{"author":"Gimly","content":"There is some decent fun to be found in _Beyond Anarchy_. It's more _Escape from LA_ than it is _Death Race 2000_, but still an entry in the franchise, which brings me to the core problem of Beyond Anarchy: Is it even really a _Death Race_ movie? The answer is yes, but to go beyond that an ask: Should it have been a _Death Race_ movie? The answer's probably no.\r\n\r\nAs I said to begin with, I had some fun with the movie, but the things that kept bringing it down were its awkward, half-hearted attatchments to the movies in the series that had gone before it. If they had have abandoned those sentiments completely, it probably would have made a better viewing experience, but then, if that had been the case, how could you call it _Death Race 4_? The opposite approach probably would have worked too, having Beyond Anarchy be an actual sequel that follows _Death Race 3_ and what came before in a way that makes sense, but then, it couldn't have been even close to the movie that we got.\r\n\r\nInstead we have _Beyond Anarchy's_ sequel-limbo status, a movie that I don't regret watching, but that also can't really work for people who are fans of the _Death Race_ franchise, or for people who have never even seen a Death Race movie.\r\n\r\n_Final rating:★★½ - Had a lot that appealed to me, didn't quite work as a whole._","id":"5af426b50e0a2639430091df","url":"https://www.themoviedb.org/review/5af426b50e0a2639430091df"}],"total_pages":1,"total_results":1}'

por lo que debe hacer JSON.parse para que el cuerpo cambie de cadena a objeto (la inserción de datos en mongo debe ser un objeto)

 let url = 'https://api.themoviedb.org/3/movie/401478/reviews?api_key=4d9c9de3bdf0d3b6837c49c086e3b190'; request(url, function (error, response, body) { body = JSON.parse(body); db.collection('data').insert(body, (err, result) => { // }); });
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda