0

Not calling the nodejs restapi from angular services.

users.component.ts

    editUser(){
    const updatedUser ={
      _id: this._id,
      name: this.name,
      email: this.email,
      mobile: this.mobile
    }
    console.log(updatedUser)
    this.usersService.updateUser(updatedUser)
    this.readUsers();
  }

users.service.ts

 updateUser(updatedUser){
    return this.http.post('http://localhost:3000/edit/' + updatedUser._id, updatedUser).pipe(
      map(res => res.json())
    )
  }

nodejs restapi

userRoutes.js

  const edituser = []
router.post('/edit/:id', (req, res) => {
    console.log('fromEditRoute')
    User.findByIdAndUpdate({_id : req.params.id}, req.body)
    .then(result => {
        edituser.push(result)
        res.json(edituser)
    })
    .catch(error => {
        console.log(error);
        res.status(500).json({message: 'An Error Occured'});
    })
    console.log('hello')
})

console.log() in routes is not even printed.

Every help will be appreciated, Thank You!!

5
  • semicolons are good :) Q: Can you communicate with any API in your NodeJS server? For example, can you use Postman with any GET or POST in the api? Commented Dec 24, 2019 at 8:33
  • In postman Api is working.. Commented Dec 24, 2019 at 8:34
  • 1
    do you have a call to updateUser with subscription? show us the call to this function Commented Dec 24, 2019 at 8:34
  • can you share your proxy.conf.js/json file? Commented Dec 24, 2019 at 8:38
  • I forget .subscribe(). Thanks community. Happy coding. :) Commented Dec 24, 2019 at 9:06

3 Answers 3

2
this.usersService.updateUser(updatedUser).subscibe(console.log);

You need to subscribe to fire the request. Observables are lazy by default. Unless you subscribe the execution doesn't start.

Sign up to request clarification or add additional context in comments.

2 Comments

It worked, how can i forget subscribe...Thank you for help.
np. it happened to me also in past :P. Please upvote if it has helped you. Thanks
0

users.component.ts

   editUser(){
        const updatedUser ={
          _id: this._id,
          name: this.name,
          email: this.email,
          mobile: this.mobile
        }
        console.log(updatedUser)
        this.usersService.updateUser(updatedUser).subscribe((response: any)=> {
        console.log(response);
       })
        this.readUsers();
     }

Comments

0

Issue is because you are not subscribing to the API call. Pipe wont subscribe to the observerble. Once you do the subscription only it will execute the call. Otherwise it will hang without giving an response. Thats why you dont get the anything as the response

Change your user service as follows:

updateUser(updatedUser){
    return this.http.post('http://localhost:3000/edit/' + updatedUser._id, updatedUser).subscibe(console.log);
  }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.