解析新用户保存不会返回新ID

时间:2015-09-28 10:43:27

标签: javascript parse-platform cloud-code

在Parse中保存新用户时,虽然用户保存正确,但保存后不会返回新用户,因此console.log("userResult id " + userResult.id);处的代码错误。如果用户已存在,则save函数会正确返回id。这是Parse的错误还是我错过了什么?

    return userQuery
        .first(function (userResult) {
            if (!helpers.isDefined(userResult)) {
                userResult = new Parse.User();
                userResult.set("username", helpers.phoneToUsername(phoneNumber));
                userResult.set("password", helpers.generatePassword(phoneNumber));
                userResult.set("displayName", displayName);
            }
            userResult.set("location", location);
            userResult.set("smsVerified", false);
            return userResult.save()
        })
        .then(function (userResult) {
            console.log("userResult id " + userResult.id); <-- ERROR HERE
            user = userResult;
            return Verification.createSmsVerification(userResult);
        })...

错误:

Result: TypeError: Cannot read property 'id' of undefined
    var userQuery = new Parse.Query(Parse.User);
        userQuery.equalTo("username", helpers.phoneToUsername(phoneNumber));

就好像用户在then()回调之后被保存一样,但情况并非如此。

2 个答案:

答案 0 :(得分:0)

尝试归还:

  return userQuery
        .first(function (userResult) {
            if (!helpers.isDefined(userResult)) {
                userResult = new Parse.User();
                userResult.set("username", helpers.phoneToUsername(phoneNumber));
                userResult.set("password", helpers.generatePassword(phoneNumber));
                userResult.set("displayName", displayName);
            }
            userResult.set("location", location);
            userResult.set("smsVerified", false);
            return userResult.save()
        })
        .then(function (userResult) {
            console.log("userResult id " + userResult.id); <-- ERROR HERE
            user = userResult;
            return Verification.createSmsVerification(userResult);
        })...

答案 1 :(得分:0)

经过一段时间在黑暗中摆弄这个,我发现问题是初始代码在first()回调中而不是那个。将代码的顶行从first(function()...更改为first().then(function()已解决了这个问题。

return userQuery.first()
        .then(function (userResult) {
            if (!helpers.isDefined(userResult)) {
                userResult = new Parse.User();
                userResult.set("username", helpers.phoneToUsername(phoneNumber));
                userResult.set("password", helpers.generatePassword(phoneNumber));
                userResult.set("displayName", displayName);
            }
            userResult.set("location", location);
            userResult.set("smsVerified", false);
            return userResult.save()
        })
        .then(function (userResult) {
            console.log("userResult id " + userResult.id); <-- ERROR HERE
            user = userResult;
            return Verification.createSmsVerification(userResult);
        })...