首页 > 解决方案 > 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);
            }
        }

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

标签: c#reactjsfirebase-realtime-databasefire-sharp

解决方案


我从来没有使用过火锐,但这是我的猜测

您正在调用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 数据库回调函数,以便在设置数据后调用它


推荐阅读