简体   繁体   中英

Can't get parameter value to insert into javascript string

I am using Parse.com with Twilio and I'm extremely close to fixing this problem. Right now I am using the following javascript code for my Parse.com cloud code (Account ID and Auth Token omitted):

// 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"); }
});
});

This works PERFECTLY. The purpose of this code is to send an SMS text message to my cell phone.

I get a text message to my phone and the "Body" value of " request.params.verificationCode " shows up in my text message as a 4 digit code like 5592.

Here is the problem though. I need to change the actual message so that when you look at it on your phone it reads "Here is your 4 digit code: 5592" but I cannot get that to work.

I can either use a sentence in parentheses by itself eg. "Here is your 4 digit code", or just use " request.params.verificationCode " by itself but I cannot get the 2 to work together.

I have been trying for over an hour now to get this to work. Have searched on google and SO for "how to insert variable into string" and I have followed the steps like inserting request.params.verificationCode into a variable and then trying to place a variable inside the body sentence(string) but I get errors every time I save and try to send the text message.

Any help with this would be greatly appreciated and I apologize if any of my terminology is off but I'm only experienced with objective-c and this is my first time ever dealing with javascript.

Thank you for the help.

You should be able to combine your custom text with the verification code using the string concatenation operator ( + ):

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

If request.params.verificationCode is a Number then it will be implicitly converted to a string before it is concatenated, otherwise it will just be concatenated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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