0

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.

Link to the tutorial here.

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)
Lucas Huang
  • 3,998
  • 3
  • 20
  • 29
Timo Cengiz
  • 3,367
  • 4
  • 23
  • 45

1 Answers1

1

The error is pretty straightforward. You use user to call the set method. And it says that TypeError: Cannot call method 'set' of null. So, the problem is about you called user. Try to debug the user or check whether Parse.User.current() return proper user.

Lucas Huang
  • 3,998
  • 3
  • 20
  • 29
  • Lol, what i noticed now is that i did not have a user so that is why it is returning null. What i want to achieve is that when a new user is using the application he/she will need to validate by providing a phone number and nothing else. And after validated i would then create a user for that person.But using the method im using now needs a user before so how would i be able to solve this? any suggestions would be appreciated @lucas Huang – Timo Cengiz Jan 18 '15 at 21:42
  • I think YES. You probably solve how to get the user before doing any operations. – Lucas Huang Jan 18 '15 at 22:27