You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何处理代码运行时出现的‘auth/user-not-found’错误?

解决方法:筛选出触发auth/user-not-found的无效电话号码

嘿,我来帮你调整这段代码,精准捕获那些找不到对应用户的无效电话号码~

原来的代码里,getAuth函数的catch只是打印错误,没法把错误和对应的电话号码关联起来,所以我们需要修改逻辑,让每个号码的请求结果都带上自身的标识,这样就能轻松筛选出无效的那些。

这里是调整后的完整代码:

const user = async(acticityObject) => { 
  const phoneNumberArray = ['+91555555555','+915585565555']; 
  
  // 重写getAuth,返回包含号码和存在状态的对象
  const getAuth = async phoneNumber => { 
    try {
      // 尝试获取用户
      await auth.getUserByPhoneNumber(phoneNumber);
      // 成功则标记为存在
      return { phoneNumber, exists: true };
    } catch (error) {
      // 只处理用户不存在的特定错误
      if (error.code === 'auth/user-not-found') {
        return { phoneNumber, exists: false };
      }
      // 其他错误(比如权限问题、网络错误)可以选择抛出或自定义处理
      throw error;
    }
  }; 

  // 批量处理所有号码请求
  const userRecords = await Promise.all(phoneNumberArray.map(getAuth)); 
  
  // 从结果中筛选出无效号码
  const invalidPhoneNumbers = userRecords
    .filter(record => !record.exists)
    .map(record => record.phoneNumber);
  
  // 这里可以根据你的需求使用invalidPhoneNumbers变量,比如打印、存储等
  console.log('找不到用户的无效号码:', invalidPhoneNumbers);
  
  // 可以返回无效号码列表供外部使用
  return { invalidPhoneNumbers };
} 

module.exports = {user}

关键调整点说明:

  • 把getAuth改成返回包含phoneNumber和exists状态的对象,这样每个请求结果都能和原号码对应上
  • 在catch里判断错误码,只把auth/user-not-found的情况标记为无效号码,其他错误可以保留抛出逻辑(避免隐藏其他问题)
  • 通过filter和map从批量结果中提取出所有无效号码,存入invalidPhoneNumbers变量供你后续使用

这样一来,你就能准确拿到所有找不到用户的电话号码啦~

内容的提问来源于stack exchange,提问作者amarjeet kumar singh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 19:54:10