Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.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
如何使用ReactJS增加Firebase中的值_Reactjs_Firebase_React Native_Google Cloud Firestore - Fatal编程技术网

如何使用ReactJS增加Firebase中的值

如何使用ReactJS增加Firebase中的值,reactjs,firebase,react-native,google-cloud-firestore,Reactjs,Firebase,React Native,Google Cloud Firestore,我想当我按下某个项目时,将其值增加x个时间量 我有一个名为“watchtime”的值和它的as编号,这是当我点击项目时的代码,它将带我进入项目屏幕 <View style={{ backgroundColor: "#f7f7f7" }}> {this.state.RecipeArr.map((item, i) => { return ( <View>

我想当我按下某个项目时,将其值增加x个时间量

我有一个名为“watchtime”的值和它的as编号,这是当我点击项目时的代码,它将带我进入项目屏幕

<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>

我的问题是,当我单击某个项目时,如何增加“Watchtime”值。

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

var videoRef=db.collection('video').doc('video_id');
videoRef.update({
监视时间:firebase.firestore.FieldValue.increment(1234)
});
也可以使用负数递减

videoRef.update({
监视时间:firebase.firestore.FieldValue.increment(-200)
});

你看到了吗?@Frank van Puffelen我一定错过了,谢谢!
 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!");
      }
    });
  }