【问题标题】:Skip Exception Firebase Javascript跳过异常 Firebase Javascript
【发布时间】:2017-08-25 12:20:17
【问题描述】:

我正在尝试使用给定的电子邮件和密码列表通过循环 (JS) 将 100 名用户注册到 firebase。

但其中很少有已经存在,而且很少有电子邮件格式错误。因此,如果用户电子邮件已注册或格式错误,我编写的脚本会抛出异常。我想跳过该用户的注册并继续下一个。

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

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
     });
  }
}

【问题讨论】:

    标签: javascript firebase firebase-authentication


    【解决方案1】:

    你可以使用 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');
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-26
      • 1970-01-01
      • 2021-04-03
      • 2013-06-03
      • 1970-01-01
      • 2022-01-01
      相关资源
      最近更新 更多