我有一个firestore Db,其中存储了用户的电子邮件列表,也有一个表单供用户填写和提交,其中有一个检查现有的电子邮件。
<JS代码/strong>
db.collection( 'emails' ).where( email.value,'!=', 'DbEmail' ).onSnapshot( ( querySnapshot ) =>
{
if ( querySnapshot.empty )
{
console.log( 'new record' )
}
else
{
console.log( 'old record.' )
}
无论何时执行查询,它都会正常工作并给出正确的输出-
新纪录
当我输入一个在数据库中不可用的电子邮件时。
问题:
当我输入一个现有的电子邮件时,我得到相同的输出即'new record'不正确。
它应该执行'old record',但它没有发生。
如何解决这个问题?
根据我的理解,你的代码中有两个错误:
1/where()
方法的签名如下:
where ( fieldPath : string | FieldPath , opStr : WhereFilterOp , value : any ) : Query < T >
2/您想要检查给定的电子邮件是否已经存在,因此您应该使用"=="
操作符。
所以我觉得你应该改成:
db.collection('emails').where('DbEmail', '==', email.value ).onSnapshot((querySnapshot) => {
if (querySnapshot.empty) {
console.log('No record corresponding to this email')
}
else {
console.log('A record with this email already exists.')
}
});