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.
Source: stackoverflow.com
Related Query
- How would I resolve this 422 error during POST?
- How can I solve this 422 error when trying to send information to the database using an axios POST request?
- How to fix this error : " Module not found :can't resolve popper.js "
- What is this error coming from source-map-loader and how do I resolve it?
- how to fix this Error Cannot use the same canvas during multiple render() operations
- How can i resolve this error in React+Material UI
- How Do I Fix this Sequelize Database Error when trying to do a POST method?
- how to solve this Cannot POST error in MERN?
- How to resolve this error ---> TypeError: Cannot read property 'id' of null
- How to resolve this reference error : Audio is not defined
- How can i resolve the syntax error from this code
- How Can I Resolve this error for Installing npm install --save react-html-table-to-excel this packages
- How can I resolve this error " type is invalide" in react native
- I'm trying to make a POST request to get receive access token, however I'm receiving undefined. How can I resolve this please?
- How to resolve this `t9n` translations error when I use its `plural` property?
- How to resolve the error on 'react-native start'
- How to catch and handle error response 422 with Redux/Axios?
- Error: Not implemented: window.scrollTo. How do we remove this error from Jest test?
- How to solve this error Can not find utils in context
- Minified React error #152 - how to resolve this?
- How to resolve ' error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature.'?
- how to solve this error You may need an appropriate loader to handle this file type
- Why does this error exist: "Invariant Violation: Cannot update during an existing state transition"
- How to resolve the no-undef error for setTimeout
- React Native start failed with error code -4094 how to solve this ONCE for ALL?
- How to resolve CORS error in REACT with ASP.NET Core
- How to resolve 'this navigator has both navigation and container props' error
- How to resolve ':react-native-vector-icons' error for a successful build?
- how to fix this error while installing radium packages in vs code terminal?? thanks
- How to avoid "Excessive number of pending callbacks: 501" error during images download in React Native?
More Query from same tag
- Dynamic CSS stylesheets based using State Values in React
- How to create a wrapper for an Apollo query that uses React hooks
- reset component's internal state on route change
- Mapping Object Array Data in Redux
- Having trouble displaying values retrieved from multiple APIs to Ag-Grid rows (React)
- React Native Read User Email Without Prompting
- How to show date correctly in react?
- multiple dropdown state and handling outside click in React
- GatsbyJS - Using SVGs as pseudo background image in CSS
- Problem with card image load in Electron ReactJS and Material-Ui
- Reusable React Map Method for Different Selected Arrays
- Create an interactive SVG component using React
- How to get state data from child to parent component?
- How can you show a modal in react when you press the back button browser?
- override component mui v5 react
- How to prevent non-browser clients from sending requests to my server
- React Duplicate components updating wrong state: hooks
- Conditional rendering of a static partial in react - MERN
- Why is _this.state.searchAddress() not a function?
- Overriding TableRow hover CSS on Material-ui in React
- How to set up redux-persist with middlewares properly?
- How to remove item from array 1 and push item to array 2
- How to use view logic to determine a component's props
- How to remove only one of repeated chars in string using JavaScript
- React execute common code on each route change
- How to edit a form when it is loaded with values?
- Enzyme shallow testing with Material-UI
- Setup multiple .env configurations (React/Next/Node)
- setState not working inside axios fetch
- how to fix this npm start not working ( after npx create-react-app )