【问题标题】:How to fix TypeError: Converting circular structure to JSON at JSON.stringify (<anonymous>) in react-native如何修复 TypeError:在 react-native 中的 JSON.stringify (<anonymous>) 处将循环结构转换为 JSON
【发布时间】:2019-04-30 09:52:10
【问题描述】:

我需要将 firebase 身份验证令牌作为用户存储在 react native asyncstorage 中。这是我的代码。

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>)

谁能帮我解决这个问题?

【问题讨论】:

  • 只需在调用this.storeUser(JSON.stringify(user));时将 JSON.stringify(user) 更改为 user 即可
  • 试过了。但不工作。仍然有同样的错误。

标签: javascript react-native firebase-authentication react-native-android


【解决方案1】:

signInWithEmailAndPassword(email, pw) 函数调用的用法不正确,firebase.auth.signInWithEmailAndPassword() 函数返回一个 Promise&lt;UserCredential&gt; 对象,当您在其上调用 JSON.stringify() 时会出错。

返回的UserCredential对象需要使用Promise类的then()方法从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');
    });
}

免责声明:由于没有显示整个功能,请以上面的方式玩

【讨论】:

    【解决方案2】:

    从上面的代码错误是转换两次

    等待 AsyncStorage.setItem('User', user).then(() => {

    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);
            }
        }
    

    【讨论】:

    • 试过了。但不工作。仍然有同样的错误。
    猜你喜欢
    • 2016-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-03
    • 2017-06-29
    • 1970-01-01
    相关资源
    最近更新 更多