如何在 Firebase .onSnapshot() 中设置数据

How to set data in Firebase .onSnapshot()

我正在尝试使用此函数设置数据,但它没有设置变量。我如何设置信息以供外部使用?

这是我的代码

const docRef = store.collection('users').doc(item.email)
var image;
docRef.onSnapshot((doc) => {
    if (doc.exists) {
        image = doc.data().picture
    }
})

如何设置此函数中的图像变量以在 onSnapshot 之外使用?这是一个 React JS 项目。

在 React 中你应该处理状态中的数据

import React ,{useState} from 'react';

const [image,setImage] = useState('');
 const docRef = store.collection('users').doc(item.email);
 docRef.onSnapshot((doc) => {
     if (doc.exists) {
        setImage(doc.data().picture) // Set Image Here
     } })

现在您可以在 onSnapshot scoop 之外使用图像 const