![](/img/trans.png)
[英]Angular, TypeError: Cannot read property 'toLowerCase' of undefined
[英]Angular: TypeError: Cannot read property 'toLowerCase' of undefined
我的問題是,當我使用 http.post 方法時,它給了我一個錯誤“TypeError:無法讀取未定義的屬性 'toLowerCase'”。 問題是什么? 我嘗試將 Angular 降級到 8.0.0 版,但它不起作用。
我的登錄服務:
login(model: any)
{
return this.http.post(this.baseUrl, model)
.pipe(map((response: any) => {
const user = response;
if(user){
localStorage.setItem('token', user);
}
}));
}
我在組件中的登錄方法:
login(){
this.authService.login(this.model)
.subscribe(next =>{
console.log('You have been logged in');
},err => {
console.log(err);
});
}
我認為您的問題與baseUrl
聲明有關。 您可能正在定義它,但不是設置值而是設置類型。 請記住:
設置類型和=
值,您可能知道這一點,但我將其寫下來以供將來參考。 所以你的代碼中可能有這樣的東西:
private baseUrl: 'http://localhost:3000/';
如果我使用 console.log baseUrl's
類型,我當然會得到未定義:
console.log(typeof this.usersUrl);
// returns undefined
如果我使用 console.log baseUrl
來獲取它的值,我們將再次未定義:
console.log(this.usersUrl);
// returns undefined
檢查這個例子。
如果是這種情況,只需修復baseUrl
的聲明並將字符串設置為值:
private baseUrl: string = 'http://localhost:3000/';
關於為什么錯誤指向 toLowercase 方法的一些見解
您收到TypeError: Cannot read property 'toLowerCase' of undefined"
錯誤,因為 angular 試圖使用本機方法toLowercase將字符串baseUrl
轉換為所有小寫字母(以防萬一),並且由於它實際上未在您的代碼中正確定義,baseUrl未定義或字符串,因此拋出錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.