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

228
Vistas
Validation error for array using axios mongodb

My Article Schema contains tags field.

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

My Pug Template for that specific field inside the form

 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
 

My javaScript file to handle POST request for sending data to the axios function in 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);

PostArticle handler function I'm using axios to post the data to the API but I can't post the data

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);
  }
};

my error message in the console after i submit the form

message: 'Article validation failed: tags.0: Article must have tags'
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I found it while checking form.append methods

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

this is how array of objects with same name should be sent to database

about 4 years ago · Juan Pablo Isaza 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