I'm developing a new App where I request users to sign up via the phone number, so that I can store it in Parse.com's database. I am doing it to use the same phone numbers to send push notifications.
I have been trying to accomplish this by using Twilio and Parse.com. I read the tutorial here and I tried to follow it without success.
This is my cloud code (as seen in the URL linked above )
var twilio = require('twilio')('removed', 'removed');
Parse.Cloud.define("sendVerificationCode", function(request, response) {
var verificationCode = Math.floor(Math.random()*999999);
var user = Parse.User.current();
user.set("phoneVerificationCode", verificationCode);
user.save();
twilio.sendSms({
From: "+46 10 138 91 84",
To: request.params.phoneNumber,
Body: "Your verification code is " + verificationCode + "."
}, function(err, responseData) {
if (err) {
response.error(err);
} else {
response.success("Success");
}
});
});
Parse.Cloud.define("verifyPhoneNumber", function(request, response) {
var user = Parse.User.current();
var verificationCode = user.get("phoneVerificationCode");
if (verificationCode == request.params.phoneVerificationCode) {
user.set("phoneNumber", request.params.phoneNumber);
user.save();
response.success("Success");
} else {
response.error("Invalid verification code.");
}
});
And this is the code I call in Xcode:
NSString *number = @"0737879108";
NSDictionary *params = [NSDictionary dictionaryWithObject:number forKey:@"number"];
//NSLog(@"%@", params);
[PFCloud callFunctionInBackground:@"sendVerificationCode" withParameters:params block:^(id object, NSError *error) {
NSString *message = @"";
if (!error)
message = @"Your SMS invitation has been sent!";
} else {
message = @"Uh oh, something went wrong :(";
}
[[[UIAlertView alloc] initWithTitle:@"Invite Sent!"
message:message
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil, nil] show];
}];
Why am I getting this error message?
TypeError: Cannot call method 'set' of null
at main.js:7:10 (Code: 141, Version: 1.6.1)