Parse JS SDK查询类遇Permission denied,用户认证状态异常求助
Let's break down your problem step by step—your login appears to succeed, but the user isn't being recognized as authenticated/current, which is almost certainly why your Person class query is getting blocked by CLPs. Here's what to check and fix:
1. Fix the Current User State
The red flag here is user.isCurrent() and user.authenticated() returning false after login. In most cases, Parse's logIn() method should automatically set the user as the current session user, but sometimes (especially with SDK version quirks or initialization issues) this doesn't happen. Try manually setting the current user right after login:
Parse.initialize('YOUR_APP_ID', 'YOUR_JAVASCRIPT_KEY'); Parse.serverURL = 'http://your-server-url/parse'; // Double-check this matches your Parse Server config! const Person = Parse.Object.extend('Person'); // Note: Registering the subclass is optional unless you need custom methods—you can also just use the class name directly in queries Parse.User.logIn('username', 'password').then(user => { // Explicitly set the current user Parse.User.current(user); console.log('E-mail: ' + user.getEmail()); console.log('Created: ' + user.createdAt); console.log('Current: ' + user.isCurrent()); // Should now return true console.log('Authenticated: ' + user.authenticated()); // Should now return true const query = new Parse.Query(Person); query.equalTo('name', 'John'); return query.find(); }).then(results => { console.log('Found users:', results); }).catch(err => { console.error('Error details:', err.message); });
2. Verify Your Class Level Permissions (CLPs)
Make sure your CLP setup for the Person class is actually targeting the correct user:
- Go to Parse Dashboard > Your App > Person Class > Permissions
- For the Find action, select "Only these users"
- Add the objectId of your manually created user (not the username—this is a common mistake!)
- Double-check that Read and Write actions are also configured correctly if you need them
3. Check Parse Server & SDK Compatibility
Outdated SDK versions can cause session handling bugs. Ensure you're using the latest stable version of the Parse JS SDK (you can update via npm install parse@latest or the CDN equivalent). Also, confirm your Parse Server configuration has session support enabled (it's enabled by default, but it never hurts to verify):
// Example Parse Server config snippet const parseServer = new ParseServer({ appId: 'YOUR_APP_ID', masterKey: 'YOUR_MASTER_KEY', serverURL: 'http://your-server-url/parse', sessionLength: 31536000, // Default is 1 year, adjust if needed // Avoid enabling enableSingleSessionPerUser unless you explicitly need it—this can cause session conflicts });
4. Validate SDK Initialization
Ensure your Parse SDK is initialized correctly before any login/query operations. Missing or incorrect serverURL, appId, or javascriptKey can silently break session handling.
Why This Works
When user.isCurrent() returns false, the query is being sent without a valid authenticated session. Parse Server checks the CLPs against the session user—if there's no valid session, it rejects the request with "Permission denied". Fixing the current user state ensures your query carries the correct user context for the CLP check.
内容的提问来源于stack exchange,提问作者vegidio

