Below is my POSTMAN headers as well as the settings I used to send my POST
. Only when I changed the Content-Type
to appplication/json
then it works.
This is my web application server side.
const express = require("express");
const bodyParser = require('body-parser');
const app = express();
const cors = require("cors");
const pool = require("./db");
//middleware
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }))
//POST
app.post("/species", async (req, res) => {
try {
console.log(req.body)
const {name, description, img} = req.body
const newSpecies = await pool.query("INSERT INTO speciesdata (name, description, img ) VALUES ($1, $2, $3) RETURNING *",
[name, description, img]
);
console.log(newSpecies)
res.json(newSpecies.rows);
} catch (err) {
console.error(err.message);
}
});
app.get('/species', async (req, res) => {
try {
const allspecies = await pool.query("SELECT * FROM speciesdata");
res.json(allspecies.rows);
} catch (err) {
console.log(err.message)
}
})
//DELETE
app.delete("/species/:id", async (req, res) => {
try {
const {id} = req.params
const deletespecies = await pool.query("DELETE FROM speciesdata WHERE id = $1",[id]);
res.json(deletespecies.rows);
} catch (err) {
console.error(err.message);
}
});
app.listen(5000, () => {
console.log("server has started on port 5000")
})
My other requests such as the app.get
as well as app.delete
works. It is only the first one app.post
that is not working. When using POSTMAN, the req.body
returns me { name: 'name', description: 'description', img: 'img' }
and i can see it being displayed on my web application which is what I want.
However, on my frontend application side, upon submitting the form, nothing was being displayed both on the frontend and the backend side. This is my react client side.
function AddSpeciesForm() {
const [Nameinput, setName] = useState('');
const [Descriptioninput, setDescription] = useState('');
const [imageinput, setImage] = useState('');
const onSubmitForm = async e => {
e.preventDefault();
try {
const response = await fetch("http://localhost:5000/species", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
"name" : Nameinput,
"description": Descriptioninput,
"img": imageinput
})
});
console.log(response)
} catch (err) {
console.error(err.message);
}
};
return (
<AddSpeciesContainer>
<AddSpeciesWrapper>
<AddSpeciesform>
<AddSpeciesH1>Add new species</AddSpeciesH1>
<InputWrapper>
<NameInput
placeholder="Name"
type="text"
value={Nameinput}
onChange={e => setName(e.target.value)}
/>
<DescriptionInput
placeholder="Description"
type="text"
value={Descriptioninput}
onChange={e => setDescription(e.target.value)}/>
<ImgInput
placeholder="Image"
type="text"
value={imageinput}
onChange={e => setImage(e.target.value)}/>
</InputWrapper>
<AddButton onSubmit= {onSubmitForm}>Add</AddButton>
</AddSpeciesform>
</AddSpeciesWrapper>
</AddSpeciesContainer>
)
}
export default AddSpeciesForm
The previous time I asked this I was able to solve the POSTMAN side but right now on my application side even when I had change the Content-Type: application/JSON
it is still not working. I have been strugguling with this for the past few days, would really appreciate some help.