Firebase 跨平台问题 - .setAsync() 在 2 个路径目标中设置,而不仅仅是一个

问题描述

我在 c# winforms 中为我的应用程序使用 Firesharp。这个数据库也连接到我处理相同信息的 ReactJS 网站。

我注意到,当我在网站上对我的应用程序进行 .SetAsync 调用然后登录到我的 Winforms 应用程序时,我的 WinForms 应用程序将自动执行我在我的网站上对我的数据库执行的最后一个操作将一些用户信息添加到其他用户信息列表的 .setAsync() 操作。现在它不会停止。每当我登录到我的 c# 应用程序时,它都会运行它。

这让我觉得在firesharp中有一个订单队列?

这是我的反应代码。据我所知,这没什么不寻常的:

async function handleSubmit(e) {
        e.preventDefault()

        var date = moment().format("MM/DD/YYYY" )

        setError("")
        setLoading(true)

        // grab user info first then use that.
        await firebaseApp.database().ref("Users/" + currentUser.uid + "/UserData").on('value',snapshot => {
            if (snapshot.val() != null) {
                setContactObjects({
                    ...snapshot.val()
                })

                firebaseApp.database().ref("Projects/" + projectGUIDRef.current.value + "/queueList/" + userIdRef.current.value).set({
                    "EntryTimeStamp": date + " " + moment().format("hh:mm:ss a"),"IsSyncing": false,"UserId": userIdRef.current.value,"UserName": usernameRef.current.value,})
            }
        })

        history.push("/Demo")

        setLoading(false)
    }

这是我的 c# winforms 代码,用于说明代码的执行位置。出于某种原因,当它执行时,它也会更新反应代码EntryTimeStamp 字段并完全设置所有信息,即使我删除它也是如此。如果我运行 .set(),它也会发生。

updateLastLogin2(authLink);

private async void updateLastLogin2(FirebaseAuthLink authLink)
        {
            IFirebaseConfig config = new FireSharp.Config.FirebaseConfig
            {
                AuthSecret = this.authLink.Firebasetoken,BasePath = Command.dbURL,};
            IFirebaseClient client = new FireSharp.FirebaseClient(config);

            string newDateTime = DateTime.Now.ToString();

            if (authLink.User.displayName.Contains(adUserId) && authLink.User.displayName.Contains(adUserId))
            {
                await client.SetAsync("Users/" + this.authLink.User.LocalId + "/UserData/DateLastLogin",newDateTime);
            }
        }

感谢任何和所有帮助,我已经在这工作了一天半了。

解决方法

我从未使用过火锋,但这是我的猜测

您在反应中调用 await firebaseApp.database().ref("Users/" + currentUser.uid + "/UserData").on('value',然后在 Csharp 中调用 client.SetAsync("Users/" + this.authLink.User.LocalId

发生的情况是两个听众都在互相倾听,然后导致循环。

在这种情况下,如果您只使用一次,最好使用 once 而不是 on

如果您不能使用 .once,那么您应该在完成后使用 .off 关闭监听器。

firebaseApp.database().ref("Users/" + currentUser.uid + "/UserData").once('value'`

您也不应该在此处使用 await,因为 ref().on 创建了一个侦听器,它不会返回承诺。

您还应该将 history.push("/Demo") 移动到您的 firebase 数据库回调函数中,以便在您设置数据后调用它