reactjs - 反应。如何保存到变量中,而不是在 console.log()
问题描述
我对 React 很陌生。我可以使用 console.log() 使用以下函数看到正确的结果,但是如何将结果(它是一个数字,而不是数组)保存到一个简单的变量中。
firestore.collection('games').where('user_ide', '==', uid).get().then( (snapshot) =>
console.log(snapshot.docs.length),
);
多谢。
解决方案
你在使用状态吗?如果是这样,那么您可以做的是像这样初始化上面的状态:
const [
snapShot,
setSnapShot,
] = useState()
并使用
firestore.collection('games').where('user_ide', '==', uid).get().then( (snapshot) =>
setSnapShot(snapshot.docs.length),
);
如果它在某个地方你不能使用状态,只需使用
let snapShot
firestore.collection('games').where('user_ide', '==', uid).get().then( (snapshot) =>
snapShot= snapshot.docs.length
);
推荐阅读
- python - DataFrame 按组计算具有条件的总股票收益
- javascript - Angular Firestore 权限不足
- python - Holoviews 中的水平滚动条
- nestjs - Nest JS - 尝试在 RolesGuard 中获取用户上下文时返回未定义
- quickfixj - QuickFIX/J :设置 StartTime/EndTime 以便会话永久运行
- flutter - 无法从本地 Web 服务器获取数据
- flutter - 如何在颤动中向grpc移动客户端添加拦截器
- java - 我可以在运行时查询 lambda 函数的生命周期吗?
- javascript - 如何获取当前设置的 CSS 样式?
- python - 获取日期范围内每小时的数据