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") | gamingMi 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'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