【问题标题】:Compare string firestore with React Native比较字符串 firestore 和 React Native
【发布时间】:2021-03-16 11:01:02
【问题描述】:

我正在尝试根据任务(任务)的名称检查我要添加的任务是否已存在于 firestore(任务集合)中。当我显示 documentSnapshot.exists 时,它是未定义的

addTask=async(task)=>{
    var useruid=firebase.auth().currentUser.uid;
    console.log("task : "+task);
    try{
        await firebase.firestore().collection("Tasks").where('task','==',task).get().then(documentSnapshot=>{
        {console.log('Task exists : ',documentSnapshot.exists)}
        if(documentSnapshot.exists)
        {console.log('This task already exists!')}
        else{
          firebase.firestore().collection('Tasks').add({userProducer:useruid,task:task}).then(() => {
            console.log('Task added!');
          })
      }

    })
  }
  catch(error){
    console.log("error = "+error)
  }
  }

【问题讨论】:

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


    【解决方案1】:

    检查 documentSnapshot 长度而不是存在

    addTask = async (task) => {
      var useruid = firebase.auth().currentUser.uid;
      console.log("task : " + task);
     try {
       await firebase
      .firestore()
      .collection("Tasks")
      .where("task", "==", task)
      .get()
      .then((documentSnapshot) => {
          console.log("Task exists : ", documentSnapshot.docs.length);
        if (documentSnapshot.docs.length > 0) {
    
          console.log("This task already exists!");
        
        } else {
          firebase
            .firestore()
            .collection("Tasks")
            .add({ userProducer: useruid, task: task })
            .then(() => {
              console.log("Task added!");
            });
         }
       });
       } catch (error) {
         console.log("error = " + error);
      }
     };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 2021-03-30
      • 1970-01-01
      • 2014-09-17
      相关资源
      最近更新 更多