我需要以用户身份存储Firebase身份验证令牌,以响应本机异步存储。这是我的代码。
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', JSON.stringify(user)).then(() => {
console.log("Success user");
})
} catch (e) {
console.log(e);
}
}
但是它给了我这个错误
TypeError: Converting circular structure to JSON
at JSON.stringify (<anonymous>)
有人可以帮我吗?
答案 0 :(得分:1)
signInWithEmailAndPassword(email, pw)
函数调用的用法不正确,firebase.auth.signInWithEmailAndPassword()函数返回一个Promise<UserCredential>
对象,当您在其上调用JSON.stringify()
时,该对象会出错。
需要使用UserCredential
类的then()
方法从Promise中检索返回的Promise
对象。见下文:
try {
fb.auth().signInWithEmailAndPassword(email, pw).then(function(userCred) => {
this.storeUser(userCred);
console.log(JSON.stringify(userCred)); // so you can see a JSON string in the logs
}).catch((err) => {
alert('Invalid email or password');
});
}
免责声明:由于未显示全部功能,请按上述步骤操作
答案 1 :(得分:0)
从上述代码中,错误转换了两次
等待AsyncStorage.setItem('User',user).then(()=> {
/etc # cat /etc/fstab
# /etc/fstab: static file system information.
#
# <file system> <mount pt> <type> <options> <dump> <pass>
/dev/root / ext2 rw,noauto 0 1
proc /proc proc defaults 0 0
devpts /dev/pts devpts defaults,gid=5,mode=620 0 0
tmpfs /dev/shm tmpfs mode=0777 0 0
tmpfs /tmp tmpfs defaults 0 0
sysfs /sys sysfs defaults 0 0