Javascript 如何修复TypeError:在react native中的JSON.stringify(<;anonymous>;)中将循环结构转换为JSON
我需要将firebase身份验证令牌作为用户存储在react native asyncstorage中。这是我的密码Javascript 如何修复TypeError:在react native中的JSON.stringify(<;anonymous>;)中将循环结构转换为JSON,javascript,react-native,firebase-authentication,react-native-android,Javascript,React Native,Firebase Authentication,React Native Android,我需要将firebase身份验证令牌作为用户存储在react native asyncstorage中。这是我的密码 logiuser=async(电子邮件,pw)=>{ 如果(此.state.isConnected){ 如果(电子邮件!=''&&pw!=''){ 试一试{ 让user=fb.auth().使用email和Password(email,pw)登录。catch((err)=>{ 警报(“无效的电子邮件或密码”); }); this.storeUser(JSON.stringify(
logiuser=async(电子邮件,pw)=>{
如果(此.state.isConnected){
如果(电子邮件!=''&&pw!=''){
试一试{
让user=fb.auth().使用email和Password(email,pw)登录。catch((err)=>{
警报(“无效的电子邮件或密码”);
});
this.storeUser(JSON.stringify(user))
console.log(用户);
}捕获(错误){
console.log(错误);
}
}
}
}
storeUser=async(用户)=>{
试一试{
等待AsyncStorage.setItem('User',JSON.stringify(User))。然后(()=>{
console.log(“成功用户”);
})
}捕获(e){
控制台日志(e);
}
}
但它给了我这个错误
TypeError: Converting circular structure to JSON
at JSON.stringify (<anonymous>)
TypeError:将循环结构转换为JSON
在JSON.stringify()上
有人能帮我吗?从上面的代码看,错误转换了两次 等待AsyncStorage.setItem('User',User)。然后(()=>{
从上述代码中,错误转换了两次 等待AsyncStorage.setItem('User',User)。然后(()=>{
有一个不正确的
符号用法,使用email和password(email,pw)
函数调用时,函数返回一个Promise
对象,当您对其调用JSON.stringify()
时,该对象将出错
需要使用Promise
类的then()
方法从Promise中检索返回的UserCredential
对象。请参见以下内容:
试试看{
fb.auth().使用email和Password(email,pw)登录。然后(函数(userCred)=>{
this.storeUser(userCred);
console.log(JSON.stringify(userCred));//因此您可以在日志中看到JSON字符串
}).catch((错误)=>{
警报(“无效的电子邮件或密码”);
});
}
免责声明:由于未显示整个函数,请使用上面的有一个不正确的
符号用法,该符号使用EmailandPassword(email,pw)
函数调用,该函数返回一个Promise
对象,当您对其调用JSON.stringify()
时,该对象将出错
需要使用Promise
类的then()
方法从Promise中检索返回的UserCredential
对象。请参见以下内容:
试试看{
fb.auth().使用email和Password(email,pw)登录。然后(函数(userCred)=>{
this.storeUser(userCred);
console.log(JSON.stringify(userCred));//因此您可以在日志中看到JSON字符串
}).catch((错误)=>{
警报(“无效的电子邮件或密码”);
});
}
免责声明:由于未显示整个函数,请使用上面的只需在调用
this.storeUser(JSON.stringify(user))时将JSON.stringify(user)更改为user;
尝试了它,但不起作用。仍然有相同的错误。只需在调用this.storeUser(JSON.stringify(user))时将JSON.stringify(user)更改为user即可
尝试过。但不起作用。仍有相同的错误。尝试过。但不起作用。仍有相同的错误。尝试过。但不起作用。仍有相同的错误。
Updated Code :
loginUser = async (email, pw) => {
if (this.state.isConnected) {
if (email != '' && pw != '') {
try {
let user = fb.auth().signInWithEmailAndPassword(email, pw).catch((err) => {
alert('Invalid email or password');
});
this.storeUser(JSON.stringify(user))
console.log(user);
} catch (error) {
console.log(error);
}
}
}
}
storeUser = async (user) => {
try {
await AsyncStorage.setItem('User', user).then(() => {
console.log("Success user");
})
} catch (e) {
console.log(e);
}
}