score:1

Accepted answer

you're constructing formdata and send it (implicitly) with content-type: multipart/form-data. express-validator doesn't validate such data out-of-the box (reasoning: https://github.com/express-validator/express-validator/issues/276)

one solution would be to use https://github.com/expressjs/multer, but my recommendation is just to submit json data with correct content-type, as already you're doing for other requests:

─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
 modified: src/twotts/pages/newtwott.js
 ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
 @ src/twotts/pages/newtwott.js:39 @ const newtwott = () => {
      event.preventdefault();

      try {
 -      const formdata = new formdata();
 -      formdata.append("title", formstate.inputs.title.value);
 -      formdata.append("description", formstate.inputs.description.value);
 -      formdata.append("creator", auth.userid);
 -      await sendrequest("http://localhost:3001/api/twotts", "post", formdata, {
 +      await sendrequest("http://localhost:3001/api/twotts", "post", json.stringify({
 +        title: formstate.inputs.title.value,
 +        description: formstate.inputs.description.value,
 +        creator: auth.userid
 +      }), {
          authorization: "bearer " + auth.token,
 +        "content-type": "application/json",
        });
        history.push("/");
      } catch (err) {}

i recommend to wrap this into a helper function. this could also set the bearer token to the request headers if present.


Related Query

More Query from same tag