无法获取参数值插入javascript字符串

时间:2014-01-06 22:36:14

标签: javascript string parameters parse-platform twilio

我正在使用Parse.com和Twilio,我非常接近解决这个问题。现在我使用以下javascript代码来获取我的Parse.com云代码(省略了帐户ID和Auth Token):

// Include the Twilio Cloud Module and initialize it
var twilio = require("twilio");
twilio.initialize("MyAccountID","MyAuthToken");

// Create the Cloud Function
Parse.Cloud.define("inviteWithTwilio", function(request, response) {
// Use the Twilio Cloud Module to send an SMS
twilio.sendSMS({
From: "15978944848",
To: request.params.number,
Body: request.params.verificationCode,
}, {
success: function(httpResponse) { response.success("SMS sent!"); },
error: function(httpResponse) { response.error("Uh oh, something went wrong"); }
});
});

这完美无缺。此代码的目的是向我的手机发送短信。

我收到一条短信给我的手机,“身体”值“request.params.verificationCode”在我的短信中显示为4位数代码,如5592.

这是问题所在。我需要更改实际的消息,以便当您在手机上查看时,它会显示“这是您的4位数代码:5592”,但我无法让它工作。

我可以在括号中单独使用一个句子,例如。 “这是你的4位数代码”,或者只是单独使用“request.params.verificationCode”,但我不能让2一起工作。

我已经尝试了一个多小时才能让它发挥作用。在google和SO上搜索了“如何将变量插入字符串”,我遵循了将request.params.verificationCode插入变量然后尝试将变量放入正文句子(字符串)中的步骤,但每次都会出错我保存并尝试发送短信的时间。

如果有任何帮助,我会非常感激,如果我的任何术语都已关闭,我会道歉但我只是遇到了Objective-c,这是我第一次处理javascript。

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

您应该能够使用字符串连接运算符(+)将自定义文本与验证码结合使用:

twilio.sendSMS({
    From: "xxxxxxxxx",
    To: request.params.number,
    Body: 'Here is your 4 digit code: ' + request.params.verificationCode,
}, ...

如果request.params.verificationCodeNumber,则在连接之前它将被隐式转换为字符串,否则它将被连接。

相关问题