Parse platform 解析云代码检查密钥重复
我使用Parse开发了一个iOS应用程序。当用户注册时,我想检查他的电话号码是否存在。因此,我编写了以下解析云代码:Parse platform 解析云代码检查密钥重复,parse-platform,cloud,Parse Platform,Cloud,我使用Parse开发了一个iOS应用程序。当用户注册时,我想检查他的电话号码是否存在。因此,我编写了以下解析云代码: Parse.Cloud.beforeSave(Parse.User, function(request, response) { var toSaveObjectId = request.object.get("objectId"); console.log("UserName: "); console.log(request.object.get('us
Parse.Cloud.beforeSave(Parse.User, function(request, response) {
var toSaveObjectId = request.object.get("objectId");
console.log("UserName: ");
console.log(request.object.get('username'));
//if (typeof obj.foo != 'undefined')
if (typeof toSaveObjectId != 'undefined') { //If toSavedObjectId is defined, then it will be an update.
console.log(toSaveObjectId);
response.success(); //Just allow update to perform. Sadly, we never access here.
}
else { //If not an update, meaning an insertion
var phoneNumer = request.object.get("additional"); //I use ParseUI so they use "additional" field for phone number. There is no problem with it.
//Now check duplication
var query = new Parse.Query(Parse.User);
query.equalTo("additional", phoneNumer);
query.count({ // If found 2 when signing up:Still do not allow. If found 2 when updating: Still allow.
success: function(count) {
if (count > 0) { //Found duplication
//Duplication while signing up. Do not allow
response.error("Duplicated phone number") ;
}
else { //Object to sign up is equal to object found. Meaning updating. Allow updating
response.success();
}
},
error: function() {
response.error("Some error when checking user phone number before saving");
}
});
}
});
这个方法在我注册时执行,如果我选择一个不存在的电话号码,我可以注册。但是用户不能有任何更新。我怀疑每次执行更新时都会调用beforeSave,并且它总是返回错误重复的电话号码
我确实试图通过检查来避免这种情况
var toSaveObjectId = request.object.get("objectId");
如果未定义toSaveObjectId,则它将是一个更新。所以我们应该回报成功。然而,代码仍然不起作用,我仍然得到了重复的电话号码。因此,问题在于条件:
if (typeof toSaveObjectId != 'undefined')
我的问题是:
1如何修复这种情况
现在我的日志成功了。我看到:
E2015-05-01T12:28:01.817Z]v21 before_save triggered for _User for user 1oDlr2aqi6:
Input: {"original":{"additional":"+84913037492","createdAt":"2015-05-01T12:16:20.838Z","email":"daominht@gmail.com","objectId":"1oDlr2aqi6","sessionToken":"r:RJVZ5hlp7z5gRBtnuydWkuCA1","updatedAt":"2015-05-01T12:16:20.838Z","username":"abfadsfsd"},"update":{"point":220,"promoCode":"1oDlr2aqi6576","usedPromoCode":"7UjadcDdAi43"}}
Result: Duplicated phone number
I2015-05-01T12:28:01.848Z]UserName:
I2015-05-01T12:28:01.849Z]abfadsfsd
在
编辑:
如果typeof toSaveObjectId!='未定义的“到”
if (toSaveObjectId != null)
但不起作用。我只是尝试console.log来获取一些request.object.get'column name'。奇怪的是,只有console.logrequest.object.getusername才能正常工作。如果我想打印request.object的其他列,我将始终获得以下日志:云代码中未提供任何消息 最后,这是我的工作代码:
Parse.Cloud.beforeSave(Parse.User, function(request, response) {
//console.log(request.object.isNew()); //You could also use this. request.object.isNew() return yes if the request try to insert new record rather than updating.
if (request.object.id != null) { //If toSavedObjectId is defined, then it will be an update
response.success(); //Just allow update to perform
}
else { //If not an update, meaning an insertion
var phoneNumber = request.object.get("additional");
if (phoneNumber == null || typeof phoneNumber == 'undefined') { //phoneNumber == null or undefined mean not signing up with phoneNumber. So let it sign up.
response.success();
}
else {
//Now check duplication
var query = new Parse.Query(Parse.User);
query.equalTo("additional", phoneNumber);
query.count({ // If found 2 when signing up:Still do not allow. If found 2 when updating: Still allow.
success: function(count) {
if (count > 0) { //Found duplication
//Duplication while signing up. Do not allow
response.error("Duplicated phone number") ;
}
else { //Object to sign up is equal to object found. Meaning updating. Allow updating
response.success();
}
},
error: function() {
response.error("Some error when checking user phone number before saving");
}
});
}
}
});
有些外卖不是:
1对于request.object的某些属性,不要使用.getkey。例如:对于objectId,不要使用.getobjectId。改用request.object.id
2如果您将某个对象(例如console.logrequest.object)保存为console.log,则在尝试使用指向新的未保存对象的指针保存某个对象时,可能未捕获。记录新创建的对象时。有人能详细解释一下吗
3如果尝试在某个空/未定义的变量上运行console.log,则不会执行下一行代码。因此,您可能无法返回成功/错误
4您可以使用request.object.isNew作为另一种方式来检查此请求是否尝试创建新对象返回yes或尝试更新现有的返回no。最后,这是我的工作代码:
Parse.Cloud.beforeSave(Parse.User, function(request, response) {
//console.log(request.object.isNew()); //You could also use this. request.object.isNew() return yes if the request try to insert new record rather than updating.
if (request.object.id != null) { //If toSavedObjectId is defined, then it will be an update
response.success(); //Just allow update to perform
}
else { //If not an update, meaning an insertion
var phoneNumber = request.object.get("additional");
if (phoneNumber == null || typeof phoneNumber == 'undefined') { //phoneNumber == null or undefined mean not signing up with phoneNumber. So let it sign up.
response.success();
}
else {
//Now check duplication
var query = new Parse.Query(Parse.User);
query.equalTo("additional", phoneNumber);
query.count({ // If found 2 when signing up:Still do not allow. If found 2 when updating: Still allow.
success: function(count) {
if (count > 0) { //Found duplication
//Duplication while signing up. Do not allow
response.error("Duplicated phone number") ;
}
else { //Object to sign up is equal to object found. Meaning updating. Allow updating
response.success();
}
},
error: function() {
response.error("Some error when checking user phone number before saving");
}
});
}
}
});
有些外卖不是:
1对于request.object的某些属性,不要使用.getkey。例如:对于objectId,不要使用.getobjectId。改用request.object.id
2如果您将某个对象(例如console.logrequest.object)保存为console.log,则在尝试使用指向新的未保存对象的指针保存某个对象时,可能未捕获。记录新创建的对象时。有人能详细解释一下吗
3如果尝试在某个空/未定义的变量上运行console.log,则不会执行下一行代码。因此,您可能无法返回成功/错误
4您可以使用request.object.isNew作为另一种方法来检查此请求是否尝试创建新对象返回yes或尝试更新现有的一个返回no。var toSaveObjectId=request.object.getobjectId;如果toSaveObjectId的类型为!='未定义”-这将始终被定义;改为检查空值?这是到目前为止得到的结果:1我更改为if toSaveObjectId!=无效的然后我得到了未调用的成功/错误。然后我注释掉了console.logtoSaveObjectId;,不,我有重复的电话号码。所以我只是尝试一下console.log来获取一些request.object.get'column name'。奇怪的是,只有console.logrequest.object.getusername才能正常工作。如果我想打印request.object的其他列,我将始终获得以下日志:云代码中未提供任何消息。所以我怀疑这就是原因。错误来自Parse?var toSaveObjectId=request.object.getobjectId;如果toSaveObjectId的类型为!='未定义”-这将始终被定义;改为检查空值?这是到目前为止得到的结果:1我更改为if toSaveObjectId!=无效的然后我得到了未调用的成功/错误。然后我注释掉了console.logtoSaveObjectId;,不,我有重复的电话号码。所以我只是尝试一下console.log来获取一些request.object.get'column name'。奇怪的是,只有console.logrequest.object.getusername才能正常工作。如果我想打印request.object的其他列,我将始终获得以下日志:云代码中未提供任何消息。所以我怀疑这就是原因。这个bug是来自Parse?