角度: 类型错误: 无法读取未定义的属性'toLowerCase'



我的问题是,当我使用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.logbaseUrl's类型,我当然会得到未定义的:

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

如果我console.logbaseUrl得到它的值,我们会再次得到未定义的:

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

查看此示例。

如果是这种情况,只需修复baseUrl的声明并将字符串设置为值:

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

关于为什么错误指向Lowercase方法的一些见解

您会得到TypeError: Cannot read property 'toLowerCase' of undefined"错误,因为angular正试图使用本机方法toLowercase将字符串baseUrl转换为所有小写字母(以防万一(,而且由于它实际上没有在代码中正确定义,baseUrl既没有定义也不是字符串,因此会引发错误。

相关内容

最新更新