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

146
Views
¿Por qué mi JSON no es válido al realizar una solicitud de publicación? (Postgres, Knex)

He estado jugando con esto por un tiempo, y tengo que decir que estoy bastante perplejo.

Tengo una tabla llamada "sitio_físico" y creé la columna "historial" como un tipo "json" en esta tabla.

Mi función de solicitud de API es la siguiente:

 const response = await fetch(BASE_URL + "physical_sites", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ data: jobSite }), });

Con mis datos entrantes como:

 { "physical_site_name": "Here", "physical_site_loc": "test", "created_by": "ME", "status": "Active", "history": [{ "action_date": "2022-07-21T01:22:44.056Z", "action_taken": "Create Job Site", "action_by": "me", "action_by_id": 24, "action_comment": "Initial Upload", "action_key": "1jt9JPRLy7RHJUwmz3kqoy98u" }] }

Continuamente agregaré elementos a la 'matriz' en la historia, por lo que habrá múltiples objetos aquí. Revisé esto en un validador JSON en línea y parece correcto.

Por último, mi función "crear" en el controlador es la siguiente:

 async function create(req, res) { const result = req.body.data; console.log(result); const data = await knex("physical_sites") .insert(result) .returning("*") .then((results) => results[0]); //insert body data into assets res.status(201).json({ data }); }

Bastante simple.

Sin embargo, sigo recibiendo este error:

 message: 'insert into "physical_sites" ("created_by", "history", "physical_site_loc", "physical_site_name", "status") values ($1, $2, $3, $4, $5) returning * - invalid input syntax for type json'

No estoy exactamente seguro de lo que está pasando, ¿alguien puede ayudarme a entender lo que me estoy perdiendo?

Gracias.

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

Lo más probable es que una de sus columnas para esta tabla tenga un tipo JSON. Creo que es la columna de history .

Puede analizar la propiedad del historial en String antes de enviar la solicitud.

 // ... body: JSON.stringify({ data: { ...jobSite, history: JSON.parse(jobSite.history) } }), // ...
about 4 years ago · Santiago Gelvez 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!