Update nested data from Angular

0

How do I update this data?:

Angular 5:

export class Cualquiera{
    constructor(
        private title: string,
        private datos: [{
            title: string,
            description: string
        }]
    ){}
}

I have the backend done with nodejs. The case is that when you try 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 data record, 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 done a thousand laps, but I have not achieved anything, I really thought that this would be less complicated, but I do not know what is 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's the controller method in nodejs. Actually, I have done this more times, but now after having tried several ways I do not know what I am doing wrong. Greetings.

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}')))
      );
    }

Back-end route:

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);
     }
   );
}
    
asked by H. Díaz 08.02.2018 в 13:13
source

1 answer

1

Make sure that before making the request to the service, the object that you send is well formed, thus ensuring that the problem is not in the angular form. Just make a console.log of the object just before the request

return this._http.put<Cualquiera>(this.url + 'updateDatos/' + id, cualquiera, httpOptions).pipe(
        tap(tap(cv => this.log('Datos actuaizados: id=${id}')))
      );

Try creating a json object by embedding object cualquiera :

let cualquieraSend= {
   'cualquiera':cualquiera
};

const httpOptions = {
      headers: new HttpHeaders({'Content-type': 'application/json'})
};

return this._http.put<Cualquiera>(this.url + 'updateDatos/' + id, cualquieraSend, httpOptions).pipe(
        tap(tap(cv => this.log('Datos actuaizados: id=${id}')))
      );

And from the backend in the method that receives the request retrieves the object of the req.body and you pass it directly to findByIdAndUpdate

let cualquieraObj=req.body.cualquiera;

Cualquiera.findByIdAndUpdate(id,establecimiento,(err, cualuqieraUpdated) => {
            if (err) { return res.send({message: err});}
            return res.send({message: 'Cualquiera actualizado.', cualquiera: cualquieraUpdated, update: cualquieraObj});
        }
    );

That's how it should work, you tell me anything.

    
answered by 08.02.2018 в 14:14