简体   繁体   English

如果 `To` 号码是否有效,如何处理错误?

[英]How to handle the error if `To` number is valid or not?

What im trying to do is to throw an error message if the user has entered an invalid number.如果用户输入了无效号码,我试图做的是抛出错误消息。

controller/auth.js控制器/auth.js

  const otp = await client.verify
    .services(serviceId)
    .verifications.create({ to: phone, channel: "sms" });
  
 // if phone is invalid{
    //return error }

  res.status(200,`The user has been created! Otp has been sent to ${phone} number!`).json({
    success: true,
    data: user,  
    otp: otp,
  });

What condition can I use to check the phone if it's invalid如果手机无效,我可以使用什么条件来检查手机

You can use the Twilio Lookup API (it has a free tier) to validate the structural integrity of the entered phone number.您可以使用Twilio Lookup API (它有一个免费层)来验证输入的电话号码的结构完整性。 You can also use the Lookup API to see what carrier the number is hosted with and if the number is a landline, mobile, or VoiP number (this functionality does have a per lookup fee).您还可以使用 Lookup API 查看该号码是由哪个运营商托管的,以及该号码是固定电话、移动电话还是 VoiP 号码(此功能确实有每次查找费用)。

Doing a lookup using the Lookup API on a invalid number format will return a 404 not found and hit the catch block.使用 Lookup API 对无效数字格式进行查找将返回 404 not found 并命中 catch 块。

client.lookups.v1.phoneNumbers('+1510867531')
                 .fetch({})
                 .then(phone_number => console.log(phone_number))
                 .catch(error => (console.log(`Error: ${error}`)))

Output:输出:

'Error: Error: The requested resource /PhoneNumbers/+1510867531 was not found'

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM