xpzouying/xiaohongshu-mcp · error
current user not found in page state
Error message
current user not found in page state
What it means
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.
Source
Thrown at xiaohongshu/login.go:62
// CurrentUser 从当前页面的 __INITIAL_STATE__ 读取登录用户信息。
// 需在 CheckLoginStatus 之后调用:复用已加载的 explore 页,不做额外导航。
func (a *LoginAction) CurrentUser(ctx context.Context) (*CurrentUser, error) {
pp := a.page.Context(ctx).Timeout(10 * time.Second)
res, err := pp.Eval(`() => {
const u = window.__INITIAL_STATE__ && window.__INITIAL_STATE__.user;
const info = u && u.userInfo && u.userInfo.value !== undefined ? u.userInfo.value : (u && u.userInfo);
if (!info || info.guest) return "";
return JSON.stringify({nickname: info.nickname, userId: info.userId || info.user_id});
}`)
if err != nil {
return nil, errors.Wrap(err, "read current user state failed")
}
raw := res.Value.String()
if raw == "" {
return nil, errors.New("current user not found in page state")
}
var user CurrentUser
if err := json.Unmarshal([]byte(raw), &user); err != nil {
return nil, errors.Wrap(err, "unmarshal current user failed")
}
return &user, nil
}
func (a *LoginAction) Login(ctx context.Context) error {
pp := a.page.Context(ctx)
// 导航到小红书首页,这会触发二维码弹窗
pp.MustNavigate("https://www.xiaohongshu.com/explore").MustWaitLoad()
time.Sleep(2 * time.Second)
View on GitHub (pinned to 332d196854)
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
Example fix
// before
user, err := login.CurrentUser(ctx)
if err != nil { return err }
// after
user, err := login.CurrentUser(ctx)
if err != nil && strings.Contains(err.Error(), "not found in page state") {
if !login.WaitForLogin(ctx) { return fmt.Errorf("login required") }
user, err = login.CurrentUser(ctx)
} Defensive patterns
Strategy: try-catch
Validate before calling
// check cookies before calling
if len(sessionCookies) == 0 { /* run login flow first */ } Type guard
func isNotLoggedIn(err error) bool {
return strings.Contains(err.Error(), "current user not found in page state")
} Try / catch
user, err := login.CurrentUser(ctx)
if isNotLoggedIn(err) {
if !login.WaitForLogin(ctx) { return fmt.Errorf("login required") }
user, err = login.CurrentUser(ctx)
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
AI-assisted analysis of xpzouying/xiaohongshu-mcp@332d196854 (2026-09-05).
Data as JSON: /api/errors/44dd03dc5954017c.
Report an issue: GitHub.