如何阻止Parse-Server自动新增对象字段至Parse仪表盘?
Sure thing! You can absolutely stop Parse from automatically creating new columns in your dashboard when you save objects with unrecognized fields. Let’s break down the different ways to do this, depending on your setup:
1. Restrict Field Creation with Class Level Permissions (CLPs)
This is the simplest approach if you’re using the managed Parse Platform or a self-hosted dashboard:
- Open your Parse Dashboard and navigate to the target class (like your "Media" class).
- Go to the Security tab, then select Class Level Permissions.
- Find the "Add Field" permission and remove it from all roles (including
public, if you don’t want any client-side code adding fields). - Now, any attempt to save an object with a new field will throw on} c� icon Type Angelaw proceeds al, it will throw an error instead of creating a new column in the dashboard.
- Pro tip: If your server-side code (like Cloud Code) needs to add fields intentionally, create a dedicated role with "Add Field" access and assign it to your server logic.
2. Enforce Field Validation with Cloud Code
For granular control over exactly which fields are allowed, use a beforeSave trigger to block invalid fields before they’re saved:
- Add this snippet to your Cloud Code:
Parse.Cloud.beforeSave("Media", async (request) => { // List all fields you explicitly allow for this class const allowedFields = ["existingField1", "existingField2", "foo"]; const objectFields = Object.keys(request.object.toJSON()); // Check for any fields not in the allowed list const invalidFields = objectFields.filter(field => !allowedFields.includes(field)); if (invalidFields.length > 0) { throw new Parse.Error( Parse.Error.INVALID_KEY_NAME, `Invalid fields detected: ${invalidFields.join(", ")}. Only ${allowedFields.join(", ")} are allowed.` ); } }); - This trigger will reject any save request that includes fields not on your allowed list, so no new columns will be created accidentally.
3. Enable Schema Locking (Self-Hosted Parse Servers)
If you’re running your own Parse Server (not the managed platform), you can lock down the entire schema to prevent any automatic changes:
- Add the
schemaLockoption to your Parse Server configuration:const parseServer = new ParseServer({ // Your existing config options... schemaLock: true }); - When schema locking is enabled, Parse Server won’t allow any schema modifications (including new fields) from saves. You’ll have to manually update the schema via your database or the dashboard’s schema editor if you need to add fields later. This is ideal for production environments where you want a strict, unchanging schema.
Which method you pick depends on your needs: CLPs are great for quick permission-based restrictions, Cloud Code gives you precise control over allowed fields, and schema locking is perfect for enforcing a rigid schema in production.
内容的提问来源于stack exchange,提问作者jaynp

