繁体   English   中英

消息未发送到 Node.js 中的用户手机号码

[英]Message is not getting sent to the user's cell number in Node.js

我想在用户向应用程序发出声音时向用户的手机号码发送 OTP 但我收到如下错误:{“消息”:“请求路径包含未转义的字符”}

我正在使用MSG91发送 OTP

function发送OTP:

exports.sendOtp = (message, phoneNumber) => {
  const options = {
    method: "GET",
    hostname: `api.msg91.com`,
    port: null,
    path: `/api/v5/otp?template_id=&mobile=${phoneNumber}&authkey=${keys.msg91}`,
    headers: {
      "Content-Type": "application/json",
    },
  };
  const otpData = http.request(options, function (res) {
    const chunks = [];
    res.on("data", function (chunk) {
      chunks.push(chunk);
    });
    res.on("end", function () {
      const body = Buffer.concat(chunks);
      console.log(body.toString());
    });
  });
  otpData.write('{ Value1: "Param1" }');
  otpData.end();
};

注册路线中的 function 调用:

exports.signup = async (req, res, next) => {
  try {
    const { phoneNumber, password, accountType, email } = req.body;
    let check_user = await Auth.findOne({ phoneNumber });
    if (check_user)
      return res
        .status(409)
        .json({ error: "Phone number is already registered" });

    const hashedPassword = passwordHash.generate(password);
    const otp = otpGenerator(4);

    sendOtp(
      {
        message: `Your OTP is ${otp}`,
        phoneNumber: phoneNumber,
      },
      next
    );
    const qrData = { phoneNumber };
    let strData = JSON.stringify(qrData);
    const generateQR = await qrcode.toDataURL(strData);
    let new_user = new Auth({
      phoneNumber,
      email,
      password: hashedPassword,
      accountType,
      otp,
      qrCode: generateQR,
    });

    const payload = {
      id: new_user._id,
      phoneNumber: `${new_user.phoneNumber}`,
    };
    let token = jwt.sign(payload, keys.secretOrKey, { expiresIn: 31556926 });
    const save = await new_user.save();

    res.send({
      success: true,
      msg: "Details saved",
      data: { user: save, token },
    });
  } catch (error) {
    return res.status(500).json({ message: error.message });
  }
};

我做错了什么?

所有 URI 字符串必须通过encodeURIComponent进行编码

path: encodeURIComponent(yourpath)

GET 请求不能包含Content-type header

暂无
暂无

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

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