How do I update this data?:
Angle 5:
export class Cualquiera{
constructor(
private title: string,
private datos: [{
title: string,
description: string
}]
){}
}
I have the backend made with nodejs. The thing is that when trying to update from an Angular form, the record is created, but not the data. I use postman as a REST application, and there I see how it generates a new record for data, but it only creates its id and does not receive the data entered.
<form #pdForm="ngForm" (ngSubmit)="onSubmit()">
<input type="text" name="title" #title="ngModel" [(ngModel)]="cualquiera.datos.title"><br />
<input type="text" name="description" #description="ngModel" [(ngModel)]="cualquiera.datos.description">
<button type="submit">Actualizar</button>
</form>
I've gone a thousand times, but I haven't achieved anything, I really thought this was going to be less complicated, but I don't know what I'm missing.
Cualquiera.findByIdAndUpdate(id,
{$push: {datos: {title: update.title, description: update.description}}},
(err, cualuqieraUpdated) => {
if (err) { return res.send({message: err});}
return res.send({message: 'Cualquiera actualizado.', cualquiera: cualquieraUpdated, update: update});
}
);
that is controller method in nodejs. The truth is, I've done this more times, but now after having tried several ways I don't know what I'm doing wrong. All the best.
EDITED:
Here is the service that makes the request to the back-end:
const httpOptions = {
headers: new HttpHeaders({'Content-type': 'application/json'})
};
.....
submitDatos(id: string, cualquiera : Cualquiera): Observable<Cualquiera>{
return this._http.put<Cualquiera>(this.url + 'updateDatos/' + id, cualquiera, httpOptions).pipe(
tap(tap(cv => this.log(`Datos actuaizados: id=${id}`)))
);
}
Backend path:
router.put('/updateDatos/:id', cualquieraController.updateDatos);
And here is the submit from the component:
onSubmitDatos(){
const id = this._route.snapshot.paramMap.get('id');
this._cualquieraService.submitDatos(id, this.cualquiera).subscribe(
response => {
// console.log(response);
},
error => {
console.log(error);
}
);
}
Make sure that before making the request to the service, the object you send is well formed, thus ensuring that the problem is not in the angular form. Simply make a
console.log
of the object just before the requestTry creating a json object by embedding the object
cualquiera
:And from the backend in the method that receives the request, retrieve the object from
req.body
and pass it directly to thefindByIdAndUpdate
This is how it should work for you, you tell me what it is.