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

99
Vistas
Can't make PUT requests from client side to Fastify backend

I'm trying to update information from the client side with a PUT request to my Fastify server, but nothing is being sent. No Errors displaying either, just no change.

POST and GET requests work fine from the client side.

PUT requests work in Insomnia/Postman.

I have fastify-cors installed:

fastify.register(require("fastify-cors"), {
  origin: "*",
  methods: "GET,POST,PUT,PATCH,DELETE",
});

Client Side Request

export default function EditUser({ route, navigation }) {
  const { item } = route.params;
  const [firstName, setFirstName] = useState(item.name[0].firstName);

  const editUser = async () => {
    try {
      const res = await fetch(`http://<myIPaddr>:5000/api/users/${item._id}`, {
        method: "PUT",
        headers: {
          Accept: "application/json",
          "Content-Type": "application/json",
        },
        body: firstName,
      });
      console.log(firstName);
    } catch (error) {
      console.log(error);
    }
  };

  return (
    <View>
      <Layout>
        <Text style={styles.text}>EDIT USER PAGE</Text>

        <View>
          <TextInput
            style={styles.input}
            onChangeText={(e) => setFirstName(e)}
            value={firstName}
          />
          <Text style={{ color: "#FFF" }}>{item._id}</Text>
          <Button title="save changes" onPress={editUser} />
        </View>
      </Layout>
    </View>
  );
}

Backend Route


  fastify.put("/:id", async (request, reply) => {
    try {
      const { id } = request.params;
      const user = await User.findByIdAndUpdate(id, request.body);
      if (!user) {
        return reply.status(400).send({ error: "couldn't find user" });
      }
      reply.status(200).send({ success: true, data: user });
    } catch (error) {
      reply.status(400).send({ error: "request failed" });
    }
  });
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Check the backend logs, it could be that you're using return reply.send. Remove the return. From the docs:

Warning:

When using both return value and reply.send(value) at the same time, the first one that happens takes precedence, the second value will be discarded, and a warn log will also be emitted because you tried to send a response twice.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Found the fix, I wasn't passing the data in the right way

const editUser = async () => {
    const userEndpoint = `http:<myIPaddr>:5000/api/users`;
    try {
      const res = await axios({
        url: `${userEndpoint}/${item._id}`,
        method: "PUT",
        data: {
          name: {
            firstName: firstName,
          },
        },
      });
      console.log(firstName);
    } catch (error) {
      console.log(error);
    }
  };
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