Как вернуть объект при обновлении через HttpClient в angular 5 - PullRequest
0 голосов
/ 09 мая 2018

Я обновил контакт, но не могу вернуть этот обновленный объект для обновления представления, вместо этого я получил {n: 1, nModified: 1, ok: 1}.

Служба получения данных из API

export class ContactService {

  url: string = 'http://localhost:3000/contacts/';

  constructor(private http: HttpClient) {
  }

  async Update(contact: Contact) {
    return await this.http.put(this.url.concat(contact._id), contact);
  }
}

ContactComponent

export class ContactComponent implements OnInit {

  async update() {
    const updatingContact = await this.contact.Update(contact);
    updatingContact.subscribe(result => {          
      console.log(result); // {n: 1, nModified: 1, ok: 1}
    })
  }
}

Вызов API

router.put('/:id', async (req, res) => {
    try {
        const contact = await Contact.updateOne({ _id: req.params.id }, {
            firstname: req.body.firstname,
            phone: req.body.phone
        });
        res.json(contact);
    }
    catch (err) {
        debug(err.message)
    }
})

Как получить обновленный объект в методе subscribe для обновления представления?

1 Ответ

0 голосов
/ 09 мая 2018

Может быть, кто-то еще встречается с этой проблемой.

Как мне улучшить мой код, чтобы получить обновленный объект

router.put('/:id', async (req, res) => { // API call

     let contact = await Contact.findById(req.params.id);
     contact.firstname = req.body.firstname;
     contact.phone = req.body.phone;

     res.json(await contact.save());
})
...