{"record":{"id":"44dd03dc5954017c","repo":"xpzouying/xiaohongshu-mcp","slug":"current-user-not-found-in-page-state","errorCode":null,"errorMessage":"current user not found in page state","messagePattern":"current user not found in page state","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"xiaohongshu/login.go","lineNumber":62,"sourceCode":"\n// CurrentUser 从当前页面的 __INITIAL_STATE__ 读取登录用户信息。\n// 需在 CheckLoginStatus 之后调用：复用已加载的 explore 页，不做额外导航。\nfunc (a *LoginAction) CurrentUser(ctx context.Context) (*CurrentUser, error) {\n\tpp := a.page.Context(ctx).Timeout(10 * time.Second)\n\n\tres, err := pp.Eval(`() => {\n\t\tconst u = window.__INITIAL_STATE__ && window.__INITIAL_STATE__.user;\n\t\tconst info = u && u.userInfo && u.userInfo.value !== undefined ? u.userInfo.value : (u && u.userInfo);\n\t\tif (!info || info.guest) return \"\";\n\t\treturn JSON.stringify({nickname: info.nickname, userId: info.userId || info.user_id});\n\t}`)\n\tif err != nil {\n\t\treturn nil, errors.Wrap(err, \"read current user state failed\")\n\t}\n\n\traw := res.Value.String()\n\tif raw == \"\" {\n\t\treturn nil, errors.New(\"current user not found in page state\")\n\t}\n\n\tvar user CurrentUser\n\tif err := json.Unmarshal([]byte(raw), &user); err != nil {\n\t\treturn nil, errors.Wrap(err, \"unmarshal current user failed\")\n\t}\n\n\treturn &user, nil\n}\n\nfunc (a *LoginAction) Login(ctx context.Context) error {\n\tpp := a.page.Context(ctx)\n\n\t// 导航到小红书首页，这会触发二维码弹窗\n\tpp.MustNavigate(\"https://www.xiaohongshu.com/explore\").MustWaitLoad()\n\n\ttime.Sleep(2 * time.Second)\n","sourceCodeStart":44,"sourceCodeEnd":80,"githubUrl":"https://github.com/xpzouying/xiaohongshu-mcp/blob/332d196854a9eac0d2b8c2c0e3d0cc43139d724c/xiaohongshu/login.go#L44-L80","documentation":"CurrentUser reads the logged-in user's state from the page via an injected JS expression; if the evaluated result string is empty it returns 'current user not found in page state'. It means the page's state does not contain the current user object — typically because the visitor is not logged in or the state shape changed.","triggerScenarios":"Calling LoginAction.CurrentUser (xiaohongshu/login.go:62) on a page whose evaluated user-state JSON is empty — the browser session has no login cookies, or the site stores user info under a different state key than the script reads.","commonSituations":"Checking the current user before completing QR-code login; cookies expired so XHS logged the session out; xiaohongshu frontend changed the __INITIAL_STATE__ user path; running against a verification/redirect page instead of the main site.","solutions":["Complete login (WaitForLogin / QR scan) before calling CurrentUser and verify cookies persist across runs","Dump the page state (or screenshot) when this happens and update the JS state path in login.go if the site renamed the user key","Load saved cookies and re-check; if still empty, treat as not-logged-in and re-authenticate","Guard the call: fall back to a login flow when this error occurs"],"exampleFix":"// before\nuser, err := login.CurrentUser(ctx)\nif err != nil { return err }\n// after\nuser, err := login.CurrentUser(ctx)\nif err != nil && strings.Contains(err.Error(), \"not found in page state\") {\n    if !login.WaitForLogin(ctx) { return fmt.Errorf(\"login required\") }\n    user, err = login.CurrentUser(ctx)\n}","handlingStrategy":"try-catch","validationCode":"// check cookies before calling\nif len(sessionCookies) == 0 { /* run login flow first */ }","typeGuard":"func isNotLoggedIn(err error) bool {\n    return strings.Contains(err.Error(), \"current user not found in page state\")\n}","tryCatchPattern":"user, err := login.CurrentUser(ctx)\nif isNotLoggedIn(err) {\n    if !login.WaitForLogin(ctx) { return fmt.Errorf(\"login required\") }\n    user, err = login.CurrentUser(ctx)\n}","preventionTips":["Persist and reload login cookies between runs","Always complete QR login before querying user state","Capture page state/screenshot to detect frontend state-path changes"],"tags":["login","session","page-state","xiaohongshu"],"backgroundTag":"not-logged-in-state","analyzedSha":"332d196854a9eac0d2b8c2c0e3d0cc43139d724c","analyzedAt":"2026-09-05T22:22:55.988Z","contentChangedAt":"2026-09-05T22:22:55.988Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}