Necesito saber cómo puedo hacer una solicitud POST al servidor json, agregando clave, par de valores a un objeto existente que no sea crear un objeto completo.
function generateUID() { return ( Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15) ); } function formatQuestion(optionOneText, optionTwoText, activeUser) { const newQuestion = { id: generateUID(), author: activeUser, timestamp: Date.now(), optionOne: { votes: [], text: optionOneText, }, optionTwo: { votes: [], text: optionTwoText, }, }; return newQuestion; } const saveQuestionHandler = async () => { const formatedQuestion = formatQuestion( optionOneText, optionTwoText, activeUser ); await fetch(`http://localhost:3001/questions/`, { method: "POST", headers: { "Content=type": "application/json", }, body: JSON.stringify(formatedQuestion["id"] : formatedQuestion), }); };Esta es mi base de datos json:
{ "questions": { "8xf0y6ziyjabvozdd253nd": { "id": "8xf0y6ziyjabvozdd253nd", "author": "sarahedo", "timestamp": 1467166872634, "optionOne": { "votes": ["sarahedo"], "text": "have horrible short term memory" }, "optionTwo": { "votes": [], "text": "have horrible long term memory" } }, } }Quiero agregar una nueva pregunta a esta base de datos para que sea como:
{ "questions": { "8xf0y6ziyjabvozdd253nd": { "id": "8xf0y6ziyjabvozdd253nd", "author": "sarahedo", "timestamp": 1467166872634, "optionOne": { "votes": ["sarahedo"], "text": "have horrible short term memory" }, "optionTwo": { "votes": [], "text": "have horrible long term memory" } }, "question ID":{ question object } } }Estoy tratando de publicar pero no puedo pasar la clave y el valor en la solicitud POST. Solo objeto completo {}