解析云代码检查密钥重复
Parse cloud code check for key duplication
我使用 Parse 开发了一个 iOS 应用程序。当用户注册时,我想检查他的 phone 号码是否存在。所以我写了下面的 Parse Cloud 代码:
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");
}
});
}
});
这个方法是在我注册的时候执行的,如果我选择一个不存在的phone号码,我可以注册。但是此时用户不能有任何更新。我怀疑每次执行更新时都会调用 beforeSave,并且它总是 returns 错误 "Duplicated phone number".
我确实尝试通过检查
来避免这种情况
var toSaveObjectId = request.object.get("objectId");
如果未定义toSaveObjectId,那么它将是一个更新。所以我们应该return成功。但是,代码仍然无法正常工作,我仍然得到 "Duplicated phone number"。所以问题出在条件上:
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
在https://parse.com/apps/(project-name)/cloud_code/log
编辑:
我将 "if (typeof toSaveObjectId != 'undefined')" 更改为
if (toSaveObjectId != null)
但也不行。我只是尝试 console.log() 一些 request.object.get('column name')。奇怪的是,只有 console.log(request.object.get("username")) 才能正常工作。如果我想打印 request.object 的其他列,我将始终在云代码中得到此日志:"No Message provided"。
最后这是我的工作代码:
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的某些属性,不要使用.get("key")。例如:对于 objectId,不要使用 .get("objectId")。请改用 request.object.id。
2) 如果你 console.log() 一些对象,例如 console.log(request.object) 你可能会在记录一个新创建的对象时得到 "Uncaught Tried to save an object with a pointer to a new, unsaved object."”。有人请详细解释一下?
3) 如果您尝试对某些 null/undefined 变量执行 console.log(),下一行代码将不会执行。所以你可能会得到 "No return success/error".
4) 您可以使用 request.object.isNew() 作为另一种方法来检查此请求是否尝试创建新对象(return 是)或尝试更新现有对象(return没有)。
我使用 Parse 开发了一个 iOS 应用程序。当用户注册时,我想检查他的 phone 号码是否存在。所以我写了下面的 Parse Cloud 代码:
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");
}
});
}
});
这个方法是在我注册的时候执行的,如果我选择一个不存在的phone号码,我可以注册。但是此时用户不能有任何更新。我怀疑每次执行更新时都会调用 beforeSave,并且它总是 returns 错误 "Duplicated phone number".
我确实尝试通过检查
来避免这种情况var toSaveObjectId = request.object.get("objectId");
如果未定义toSaveObjectId,那么它将是一个更新。所以我们应该return成功。但是,代码仍然无法正常工作,我仍然得到 "Duplicated phone number"。所以问题出在条件上:
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
在https://parse.com/apps/(project-name)/cloud_code/log
编辑: 我将 "if (typeof toSaveObjectId != 'undefined')" 更改为
if (toSaveObjectId != null)
但也不行。我只是尝试 console.log() 一些 request.object.get('column name')。奇怪的是,只有 console.log(request.object.get("username")) 才能正常工作。如果我想打印 request.object 的其他列,我将始终在云代码中得到此日志:"No Message provided"。
最后这是我的工作代码:
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的某些属性,不要使用.get("key")。例如:对于 objectId,不要使用 .get("objectId")。请改用 request.object.id。
2) 如果你 console.log() 一些对象,例如 console.log(request.object) 你可能会在记录一个新创建的对象时得到 "Uncaught Tried to save an object with a pointer to a new, unsaved object."”。有人请详细解释一下?
3) 如果您尝试对某些 null/undefined 变量执行 console.log(),下一行代码将不会执行。所以你可能会得到 "No return success/error".
4) 您可以使用 request.object.isNew() 作为另一种方法来检查此请求是否尝试创建新对象(return 是)或尝试更新现有对象(return没有)。