在_Session表中存在会话令牌时获取无效的会话令牌(代码:209) - Parse Server JS SDK

时间:2017-04-28 10:56:56

标签: parse-server

我收到无效会话令牌(代码:209)问题而我的会话令牌存在于_Session表中,我遇到此问题。 通常这个错误出现在我刚刚提出请求的数据库中不存在会话令牌doest但是在我的情况下,我在db中存在会话令牌时遇到此问题。 我只是在分享我遇到此错误的云代码。

main.js: -

Parse.Cloud.define("ping", function (req, res) {
    try {
        if (req.user !== undefined) {
            var userId = req.params.hasOwnProperty(GameConstants.USER_ID) && req.params.user_id !== "" ? req.params.user_id : req.user.get("username");
            LoginHelper.updateUserSessionEndDateTime(userId, function (error, status) {
                if (error) {
                    res.success({result: 0, custom_error_code: error.code, custom_error_message: error.message});  //Getting the error in this block :- {"result":0,"custom_error_code":209,"custom_error_message":"invalid session token"}

                    //custom log
                    logger.info(JSON.stringify({result: 0, custom_error_code: error.code, custom_error_message: error.message, req: req}));
                } else if (status === 0) {
                    res.success({result: 0, custom_error_code: CustomErrorCode.INVALID_USER_ERROR, custom_error_message: "Inavalid user"});

                    //custom log
                    logger.info(JSON.stringify({result: 0, custom_error_code: error.code, custom_error_message: error.message, req: req}));
                } else {                    
                    res.success({result: 1});

                    //CommonHelper.recordBuyerSessionData(userId);
                }
            });
        } else {
            res.success({result: 0, custom_error_code: CustomErrorCode.INVALID_USER_ERROR, custom_error_message: "Inavalid user"});

            //custom log
            logger.info(JSON.stringify({result: 0, custom_error_code: CustomErrorCode.INVALID_USER_ERROR, custom_error_message: "Inavalid user", req: req}));
        }
    } catch (e) {
        res.success({result: 0, custom_error_code: e.code, custom_error_message: e.message, stacktrace: e.stack});

        //custom log
        logger.info(JSON.stringify({result: 0, custom_error_code: e.code, custom_error_message: e.message, stacktrace: e.stack, req: req}));
    }

});

LoginHelper.js: -

updateUserSessionEndDateTime: function (userId, callback) {
        var query = new Parse.Query(Parse.Object.extend(GameConstants.GAME_USERS));
        query.select("session_end_date_time");
        query.equalTo(GameConstants.OBJECT_ID, userId);
        query.first({
            success: function (gameUser) {
                if (typeof gameUser !== "undefined") {
                    //console.log("updateUserSessionEndDateTime data===========> " + JSON.stringify(gameUser));
                    gameUser.set(GameConstants.SESSION_END_DATE_TIME, new Date());
                    gameUser.save(null, {
                        success: function (gameUser) {
                            //console.log("updateUserSessionEndDateTime.save.success===========> " + JSON.stringify(gameUser));
                            callback(null, 1);
                        },
                        error: function (error) {
                            //console.log("updateUserSessionEndDateTime.save.error===========> " + error.message);
                            callback(error);
                        }
                    });
                } else {
                    //console.log("updateUserSessionEndDateTime.sessionData-------> undefined ");
                    callback(null, 0);
                }
            },
            error: function (error) {
                //console.log("updateUserSessionEndDateTime.error===========> " + error.message);
                callback(error);
            }
        });
    }

错误日志 -

{"结果":0," custom_error_code":209," custom_error_message":"无效的会话令牌"} {&#34 ;结果":0," custom_error_code":209," custom_error_message":"无效的会话令牌"} {"结果":0 ," custom_error_code":209," custom_error_message":"无效的会话令牌"} {"结果":0," custom_error_code&# 34;:209," custom_error_message":"无效的会话令牌"} {"结果":0," custom_error_code":209,&## 34; custom_error_message":"会话令牌无效"} {"结果":0," custom_error_code":209," custom_error_message": "会话令牌无效"} {"结果":0," custom_error_code":209," custom_error_message":"会话令牌无效& #34;}

用户登录成功后再调用ping云功能。我不想删除ACL来解决此问题。我恳请你帮助我,为什么我会遇到这个问题。

由于

1 个答案:

答案 0 :(得分:0)

云代码不会自动将会话传递给后续服务器调用。如果已将ACL权限置于限制公共读/写的位置,则需要在保存,提取等时显式传递会话令牌。

即。您的保存电话应为gameUser.save(null, { success:..., error:..., sessionToken:userSessionToken});

通过在原始云功能中调用userSessionToken找到req.user.getSessionToken();