Since I Angular
send the data to nodejs
, when it receives it I ask id_user
if it is null
or undefined
in the following way:
if(!req.params.id_user){
res.status(400).send('FALTA CONTENIDO EN EL CUERPO, falta el id_user');
}
The weird thing is that since Angular
I'm sending it as id_user = 1
, so nodejs
it shouldn't respond with a res.status(400)
. The data arrives fine because if I remove the if(!req.paramas.id_user)
the data is saved perfectly in the database.
here from nodejs
theorder.controller.ts
export const createOrder = async (req: Request, res: Response): Promise<Response> => {
if(!req.params.id_user){
res.status(400).send('FALTA CONTENIDO EN EL CUERPO, falta el id_user');
}
//recibo los datos (de un form, insomnia rest, etc..)
const { id_user, adress, phone_number, total_price, provincia, localidad, order_date } = (req.body);
console.log(id_user, adress, phone_number, total_price, provincia, localidad, order_date)
let idUser = parseInt(id_user);
let totalPrice = parseInt(total_price);
const response: QueryResult = await pool.query('INSERT INTO orders (id_user, adress, phone_number, total_price, provincia, localidad, order_date) VALUES ($1, $2, $3, $4, $5, $6, $7)', [idUser, adress, phone_number, totalPrice, provincia, localidad, order_date]);
return res.json({
message: 'La orden ah sido creado exitosamente!',
body: {
orders: {
adress
}
}
})
}
Here Angular
, where I send the data to nodejs
with theid_user = 1
createOrder() {
this.order.id_user = 1;
this.order.order_date = this.currentDate;
this.order.total_price = this.total;
this.orderService.createOrder(this.order).subscribe(
res => {
console.log('La orden fue creada exitosamente');
this.createOrderDetail();
},
err => console.log('No se pudo crear la orden ' + err)
);
}
my serviceorder.service.ts
createOrder(order: Order){
return this.http.post('http://localhost:4000/orders/create', order);
}
req.params
receive the data from the url. you must do areq.body.id_user
In your service you are not sending any parameters by url. You send everything through the body.
Your code works if you remove the line because in the end you are using
req.body
: