1

My problem is that when I use http.post method it gives me an error "TypeError: Cannot read property 'toLowerCase' of undefined". What is the problem? I tried downgranding Angular to version 8.0.0 but it doesn't work.

my login service:

login(model: any)
 {
    return this.http.post(this.baseUrl, model)
    .pipe(map((response: any) => {
      const user = response;
      if(user){
        localStorage.setItem('token', user);
      }
    }));
 }

my login method in component:

login(){
    this.authService.login(this.model)
        .subscribe(next =>{
          console.log('You have been logged in');
        },err => {
          console.log(err);
        });
  }
6
  • 1
    Someone downvoted you since the question needs to be edited. Try not posting images with code, and put all relevant code in the question... the error points to toLowercase() but the method is not present in your code :) Commented Mar 12, 2020 at 15:33
  • but I haven't created method like this Commented Mar 12, 2020 at 15:35
  • 1
    @Lucas is right. Post your service code. You're blaming the success log but it's an issue with your service function. Commented Mar 12, 2020 at 15:37
  • 1
    okey, i have edited my question Commented Mar 12, 2020 at 15:40
  • you are not explaing the problem correctly, please point out the problem clearly Commented Mar 12, 2020 at 16:28

1 Answer 1

4

I think your issue is with baseUrl declaration. You are probably defining it, but not setting the value but the type instead. Keep in mind that : sets a type and = a value, you probably know this but I leave it written for future reference. So you probably have something like this in your code:

  private baseUrl: 'http://localhost:3000/'; 

If I console.log baseUrl's type, I get undefined of course:

 console.log(typeof this.usersUrl);
 // returns undefined

And if I console.log baseUrl to get its value, we get undefined again:

 console.log(this.usersUrl);
 // returns undefined

Check this example.

If this is the case, just fix the declaration of baseUrl and set the string as a value:

  private baseUrl: string = 'http://localhost:3000/'; 

Some insight as to why error points to toLowercase method

You get TypeError: Cannot read property 'toLowerCase' of undefined" error since angular is trying to convert the string baseUrl to all lowercases(just in case) with the native method toLowercase, and since it is actually not defined in your code correctly, baseUrl is not defined nor a string, so the error is thrown.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.