我正在开发一个新应用,我要求用户通过电话号码注册,以便我可以将其存储在Parse.com的数据库中。我这样做是为了使用相同的电话号码发送推送通知。
我一直试图通过使用Twilio和Parse.com来实现这一目标。我在这里阅读了教程,但我试图顺利完成它。
这是我的云代码(如上面链接的网址所示)
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.");
}
});
这是我在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];
}];
为什么我收到此错误消息?
TypeError: Cannot call method 'set' of null
at main.js:7:10 (Code: 141, Version: 1.6.1)
答案 0 :(得分:1)
错误非常简单。您使用user
来调用set方法。它说TypeError: Cannot call method 'set' of null
。所以,问题在于你叫user
。尝试调试用户或检查Parse.User.current()
是否返回正确的用户。