r/node•
ya-boi-moe

what's the best way to get the ID after creating a new object and posting it using axios?

I'm learning react from fullstackopen, and I'm wondering what's the best way I can set the ID back to the newly created object after posting it to axios?

Im doing it inside the promise

        const newPerson = {
          name: newName,
          number: newNumber
        } 
        pgonebookModule.create(newPerson).then(rs=>{newPerson.id=rs.id}) 
        setPersons(persons.concat(newPerson))
        setNewName('')
        setNewNumber('')




const create = (person) => {
    return axios.post(URL, person).then(rs => {
        return rs.data
    })
}

Posted Mar 11, 2023, 6:20 AM UTC

03
Loading comments…