I see that there are a bunch of same errors that were caused by PostgreSQL but this time I get this error when I try to perform POST action with express.js. Even though I do not leave the "name" field "null" I still somehow have the follow error; null value in column "name" in relation "restaurants" violates the not-null condition
// create a restaurant
app.post("/api/v1/restaurants", async (req, res) => {
console.log("req.body: ",req.body);
try {
const results = await db.query(
"INSERT INTO restaurants (name, location, price_range) values ($1, $2, $3) returning *",
[req.params.name, req.params.location, req.params.price_range]
);
console.log("results: ",results);
res.status(201).json({
status: "success",
data: {
restaurant: "mcdonalds",
},
});
} catch (error) {
console.log(error.message);
}
});
I filled this values in my postman;
{
"name":"Big Chefs",
"location":"Istanbul",
"price_range":5
}
the mistake was that I was trying to enter my input into the wrong request part. The correct way is so;
const results = await db.query(
"INSERT INTO restaurants (name, location, price_range) values ($1, $2, $3) returning *",
[req.body.name, req.body.location, req.body.price_range]
);
Even though I feel silly for such a mistake, I leave the question here so whoever needs that can easily solve the issue