【问题标题】:How To Increase A Value in Firebase With ReactJS如何使用 ReactJS 在 Firebase 中增加价值
【发布时间】:2021-05-26 18:49:47
【问题描述】:

我希望当我按下某个项目时将其值增加 x 时间。

我有一个名为“watchtime”的值,它是数字,当我单击将我带到项目屏幕的项目时,这是代码。

<View style={{ backgroundColor: "#f7f7f7" }}>
          {this.state.RecipeArr.map((item, i) => {
            return (
              <View>
                <TouchableOpacity
                  onPress={() => {
                    this.props.navigation.navigate("RecipeDetailScreen", {
                      Recipekey: item.key,
                    });
                  }}
                  style={styles.itemContainer}
                ></TouchableOpacity>
              </View>
            );
          })}
        </View>

这是组件安装在项目屏幕上的时间

 componentDidMount() {
    const dbRef = firebase
      .firestore()
      .collection("recipes")
      .doc(this.props.route.params.Recipekey);
    dbRef.get().then((res) => {
      if (res.exists) {
        const Recipe = res.data();
        this.setState({
          key: res.id,
          name: Recipe.name,
          category: Recipe.category,
          intro: Recipe.intro,
          watchtime: Recipe.watchtime,
          image: Recipe.image,
          ingredients: Recipe.ingredients,
          preparation: Recipe.preparation,
          isLoading: false,
        });
      } else {
        console.log("Document does not exist!");
      }
    });
  }

我的问题是当我点击某个项目时如何增加“观看时间”值。

【问题讨论】:

标签: reactjs firebase react-native google-cloud-firestore


【解决方案1】:

您可以调用 Increment for Firestore 字段触发后端读取文档并更新值。如果恶魔不存在,它会在增加您的值之前使用默认值0 创建它。

var videoRef= db.collection('video').doc('video_id');

videoRef.update({
    watchTime: firebase.firestore.FieldValue.increment(1234)
});

您也可以使用负数递减

videoRef.update({
    watchTime: firebase.firestore.FieldValue.increment(-200)
});

【讨论】:

    猜你喜欢
    • 2017-07-05
    • 1970-01-01
    • 2020-05-01
    • 2021-05-09
    • 2023-03-22
    • 1970-01-01
    • 2022-08-03
    • 2012-12-14
    • 1970-01-01
    相关资源
    最近更新 更多