我正在为基于 React Native 的移动应用程序创建 OTP 类型的注册。通过使用谷歌云功能生成 otp 并将 http 请求发布到我的 SMS 提供商。
我面临的问题是,每当我尝试使用 ${code}
将随机代码添加到我的短信提供商网址时,该消息只会显示相同的 ${code}
不是随机生成的代码。
换句话说,不知道如何将代码插入我的网址(因为我是新手)。
这是我的随机数代码:
const code = Math.floor((Math.random() * 8999 + 1000));
我使用请求包的请求如下:
const options = {
method: 'POST',
uri: 'http://smsprovider.com/numbers=${numbers}&route=2&message=Your OTP is ${code}',
body: {
numbers: phone,
code: code
},
json: true
};
因此,每当我收到消息时,都会显示您的 OTP 是 ${code}
。但我真正需要的是显示 math.floor 函数生成的随机数。预期“您的 OTP 是 5748”
请指导
最佳答案
对于使用 JavaScript 进行字符串插值,请务必使用
`
字符而不是
'
试试这个:
const options = {
method: 'POST',
uri: `http://smsprovider.com/numbers=${numbers}&route=2&message=Your OTP is ${code}`,
body: {
numbers: phone,
code: code
},
json: true
};
关于javascript - 使用 Google Cloud Function 执行 HTTP post 请求时需要在我的 URL 中添加值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54017217/