Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/434.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 嗨,我如何让firebase实时更新我的数据?_Javascript_Json_Reactjs_Firebase_Google Cloud Firestore - Fatal编程技术网

Javascript 嗨,我如何让firebase实时更新我的数据?

Javascript 嗨,我如何让firebase实时更新我的数据?,javascript,json,reactjs,firebase,google-cloud-firestore,Javascript,Json,Reactjs,Firebase,Google Cloud Firestore,我目前的设置是在页面加载时提取firebase数据,这很好,但现在我需要应用程序上的数据实时更新,我该怎么做 useEffect(() => { const user = Authentication.auth().currentUser; { user !== null && Authentication.firestore().collection('Health_data') .

我目前的设置是在页面加载时提取firebase数据,这很好,但现在我需要应用程序上的数据实时更新,我该怎么做

useEffect(() => {

    const user = Authentication.auth().currentUser;
    {
        user !== null &&
            Authentication.firestore().collection('Health_data')
                .doc(user.uid)
                .get()
                .then(doc => {
                    healthDataSet(doc.data())
                    setLoading(false)
                }).catch(function (error) {
                    console.error("Error reading health", error);
                });
    }
    return () => {
        document.body.style.overflow = 'unset';
    }

}, []);

//calculates calorie requirments
useEffect(() => {
    if (!loading && healthData !== null) {


        if (healthData.units === 'Kg') {
            BMIset(Math.round((healthData.weight / ((healthData.height / 100) * (healthData.height / 100))) * 10) / 10)


                if (healthData.gender === 'female') {
                    BMRset(Math.round(((655.1 + (9.563 * healthData.weight) + (1.850 * healthData.height) - (4.676 * healthData.age)) * 1.37) * 0.8))
                } else {
                    BMRset(Math.round(((88.2 + (13.362 * healthData.weight) + (4.799 * healthData.height) - (5.677 * healthData.age)) * 1.37) * 0.8))
                }

            }

            if (healthData.goal === 'Gain') {

                if (healthData.gender === 'female') {
                    BMRset(Math.round(((655.1 + (9.563 * healthData.weight) + (1.850 * healthData.height) - (4.676 * healthData.age)) * 1.37) * 1.2))
                } else {
                    BMRset(Math.round(((88.2 + (13.362 * healthData.weight) + (4.799 * healthData.height) - (5.677 * healthData.age)) * 1.37) * 1.2))
                }

            }

            if (healthData.goal === 'Recomp') {
                if (healthData.gender === 'female') {
                    BMRset(Math.round((655.1 + (9.563 * healthData.weight) + (1.850 * healthData.height) - (4.676 * healthData.age)) * 1.37))
                } else {
                    BMRset(Math.round((88.2 + (13.362 * healthData.weight) + (4.799 * healthData.height) - (5.677 * healthData.age)) * 1.37))
                }
这是我的代码,在数据被提取后,数据被用于计算,我想要它,所以当数据像这样更新时

  const updateWeight = () => {
        if (Weight !== null) {
            database.collection('Health_data').doc(localStorage.getItem('user')).update({
                weight: Weight
            }).catch((error) => {
                alert(error.message)
                console.log('failed to write', error);
            });
        } else {
            alert('A weight must be in put')
        }
    }
计算得到新信息,页面实时更新,这可能吗

您可以使用onSnapshot()方法收听文档。首字母 使用您提供的回调调用将创建文档快照 立即使用单个文档的当前内容。那么, 每次内容更改时,另一个调用都会更新文档 快照

如需了解更多详细信息,请点击此处:

我建议从文档开始了解它是如何工作的:这是我一直在看的网站,谢谢mate,但在堆栈溢出上的实现没有成功,建议您发布无法按预期方式工作的代码,并解释错误。谢谢,我会尝试的,我想这是我以前尝试过的,但我无法让它工作。如果它不工作,请让我知道你面临的问题在哪里?它似乎不工作,在我得到这个错误,也尝试过它的措辞,因为firebase网站建议不在反应形式,但我得到了这个错误,我肯定是我做错了什么,虽然哈哈好,所以结果是整个错误错误发生的时间,因为捕获不是快照的函数。。。