我如何在.where()中使用veriable - Firestore React Native?

时间:2018-04-09 14:44:10

标签: react-native where google-cloud-firestore

我想在Where Query中使用veriable来从User Collection获取用户数据。我使用此代码但未检索到数据:

export const userAddToOrganization = (email) => {  
    return (dispatch) => {
      if (email !== '') {
        console.log('email' , email); //'e@e.com √'
      firebase.firestore().collection('users').where("email", '==', `${email}`)
      .get()
      .then((querySnapshot) => {
        querySnapshot.forEach((doc) => {
          console.log(doc.id, "=>", doc.data());
        });
      });
    }
    };
   };

当我用作'e@e.com'时,成功检索到数据。但我想使用可靠的( 电子邮件

 export const userAddToOrganization = (email) => {  
    return (dispatch) => {
      if (email !== '') {
        console.log('email' , email);
      firebase.firestore().collection('users').where("email", '==', 'e@e.com')
      .get()
      .then((querySnapshot) => {
        querySnapshot.forEach((doc) => {
          console.log(doc.id, "=>", doc.data());
        });
      });
    }
    };
   };

如何在WHERE查询中使用veriable来获取数据。 谢谢。

1 个答案:

答案 0 :(得分:1)

export const userAddToOrganization = (email) => {  
return (dispatch) => {
  if (email !== '') {
    console.log('email' , email); //'e@e.com √'
  const ref = firebase.firestore().collection('users')
   ref.where("email", '==', email)
  .get()
  .then((querySnapshot) => {
    querySnapshot.forEach((doc) => {
      console.log(doc.id, "=>", doc.data());
    });
  });
  }
 };
};

我没有测试过,但我认为没有理由不这样做。删除字符串文字并直接传递电子邮件。如果那不起作用那么我想是修剪字符串,这将删除所有额外的空间。

export const userAddToOrganization = (email) => {  
return (dispatch) => {
  if (email !== '') {
    console.log('email' , email); //'e@e.com √'
  const ref = firebase.firestore().collection('users')

  ref.where("email", '==', email.trim())
  .get()
  .then((querySnapshot) => {
    querySnapshot.forEach((doc) => {
      console.log(doc.id, "=>", doc.data());
    });
  });
  }
 };
};