Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

129
Visualizações
Fetch POST key, value pair to json server

I need to know how can I make POST request to json-server, by adding key,value pair to an existing object other than creating full object.

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

This is my json data base:

    {
    "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"
          }
        },
      }
    }

I want to add a new question to this database to be like:

    {
    "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
        }
      }
    }

I am trying to post but I can't pass add both key and value into the POST request. Only full object {}

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda