I'm struggling to understand this..
In my requests.service I have this function that calls my backend:
loginUser(username, pw): Observable <any>{
const body = {
username: username,
password: pw
}
let headers = new HttpHeaders();
headers = headers.append("Authorization", "Basic " + btoa('test:test'));
headers = headers.append("Content-Type", "application/json");
return this.http.post('https://website.com/1/user/login', body, {headers: headers})
.subscribe((data)=>{ //use methods in our service
console.log(data)
this.userData = data;
}, (err)=> console.log(err));
}
This works, the data is returned.
Now, in login.page.ts I want to call loginUser but I also want a callback on it so I know when it has successfully been run, so I do:
this.req.loginUser(info.email, info.password).then((data) => {
console.log(data)
})
but I get the error:
this.req.loginUser(...).then is not a function
If I just console.log it with no callback it works fine, but I need to know when the call has been successful.
Any ideas?
this.req.loginUser(info.email, info.password);you will not need it..pipe(tap((data) => { ... }))if you need logic in the service and make the subscription in your component.loginUserreturn a subscription object not observable please consider look to my answer π where I explain observable and promise implementation π€π€ @JaneDoe