解析密钥重复的云代码检查

时间:2015-05-01 12:01:36

标签: parse-platform cloud

我使用Parse开发了一个iOS应用程序。当用户注册时,我想检查他的电话号码是否存在。所以我写下面的Parse Cloud Code:

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");
            }       
        });
    }
});

这个方法确实在我注册时执行,如果我选择一个不存在的phoneNumber,我可以注册。但随后用户无法进行任何更新。我怀疑每次执行更新时都会调用beforeSave,并且它总是返回错误"重复的电话号码"。

我确实试图通过检查

来避免这种情况
var toSaveObjectId = request.object.get("objectId"); 

如果未定义toSaveObjectId,则它将是更新。所以我们应该回归成功。但是,代码仍然无法正常工作,我仍然有#34;重复的电话号码"。所以问题在于条件:

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

编辑: 我改变了#34; if(typeof toSaveObjectId!=' undefined')"到

if (toSaveObjectId != null)

但不起作用。我只是尝试使用console.log()来获取一些request.object.get('列名')。很奇怪,只有console.log(request.object.get(" username"))才能正常工作。如果我想打印request.object的其他列,我将始终获得此日志:"没有提供消息"在云代码中。

1 个答案:

答案 0 :(得分:4)

最后这是我的工作代码:

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试图保存一个带有指向未保存的新对象的指针的对象。&#34 ;记录新创建的对象时#34;。有人请详细解释一下吗?

3)如果您尝试在某个null / undefined变量上调用console.log(),则下一行代码将不会执行。所以你可能会得到"没有返回成功/错误"。

4)您可以使用request.object.isNew()作为另一种方法来检查此请求是否尝试创建新对象(返回yes)或尝试更新现有对象(返回否)。