首页 文章

跳过异常Firebase Javascript

提问于
浏览
0

我试图通过循环(JS)注册100个用户到firebase与给定的电子邮件和密码列表 .

但很少有人已经存在,很少有人的电子邮件格式错误 . 因此,如果 user email already registered or is badly formatted ,我写的脚本会抛出异常 . 我想跳过该用户的注册并继续下一个注册 .

如果异常发生,如何跳过循环 .

for (var i = 0; i < data.length; i++){
//Variables of email and password
 if (email !== '' && email !== undefined) {
        const promise = firebase.auth().createUserWithEmailAndPassword(email, password)
        .then(function(response) {
           console.log(response.uid);
     });
     promise.catch(function(e){
        console.log(e.message);
        //Skip and continue to next iteration
     });
  }
}

1 回答

  • 0

    你可以使用Promise.all:

    var promises = [];
    for (var i = 0; i < data.length; i++) {
      promises.push(firebase.auth().createUserWithEmailAndPassword(email, password)
        .then(function(userRecord) {
        }).catch(function(error) {
          console.log(error).
        }));
    }
    Promise.all(promises).then(function() {
      console.log('done');
    });
    

相关问题