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

241
Views
Error de validación para matriz usando axios mongodb

El esquema de mi artículo contiene un campo de etiquetas.

 tags: { type: [String], required: [true, "A article must have a tags"], enum: { values: [ "science", "technology", "gaming", ], message: "Article must have tags", }, },

Plantilla My Pug para ese campo específico dentro del formulario

 input#checkbox1(type="checkbox", name="science", value="science") | science br input#checkbox2(type="checkbox", name="technology", value="technology") | technology br input#checkbox3(type="checkbox", name="gaming", value="gaming") | gaming

Mi archivo javaScript para manejar la solicitud POST para enviar datos a la función axios en postArticle.js

 const composeArticle = document.querySelector(".formComposePost"); if (composeArticle) { composeArticle.addEventListener("submit", (e) => { e.preventDefault(); const form = new FormData(); form.append("author", document.getElementById("userid").value); form.append("title", document.getElementById("heading").value); let tagArray = []; let science = document.getElementById("checkbox1"); if (science.checked) { tagArray.push(science.value); } let technology = document.getElementById("checkbox2"); if (technology.checked) { tagArray.push(technology.value); } let gaming = document.getElementById("checkbox3"); if (gaming.checked) { tagArray.push(gaming.value); } form.append("tags", tagArray); form.append( "imageCover", document.getElementById("image").files[0] ); postArticle(form);

Función de controlador PostArticle Estoy usando axios para publicar los datos en la API pero no puedo publicar los datos

 export const postArticle = async (data) => { try { const res = await axios({ method: "POST", url: "http://127.0.0.1:3000/api/v1/articles/", data, }); console.log(res); if (res.data.status === "success") { alert("success") window.setTimeout(() => { location.assign("/"); }, 1000); } } catch (error) { console.log(error.response.data.message); } };

mi mensaje de error en la consola después de enviar el formulario

 message: 'Article validation failed: tags.0: Article must have tags'
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Lo encontré mientras revisaba los métodos form.append

 tagArray.forEach((element) => { form.append("tags", element); });

así es como se debe enviar una matriz de objetos con el mismo nombre a la base de datos

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!