Can't use beforeSave with class containing column of type File
Describe your issue in as much detail as possible.
File objects in beforeSave are inaccessible and cause a Tried to encode an unsaved file error when simply returning a response.success()
Steps to reproduce
Please include a detailed list of steps that reproduce the issue. Include curl commands when applicable.
- Create a class which has a column "X" of type File.
- Create a beforeSave function on the class which only returns a
response.success() - Call a POST (or save()) on the class after setting values of "X"
Expected Results
Should create the Object with the File
Actual Outcome
Throws a Tried to encode an unsaved file. error at node_modules/parse/lib/node/encode.js line 63
Environment Setup
- Server
- parse-server version: 2.2.17
- Operating System: Mac OSX 10.11.5 (local NodeJS)
- Hardware: MacBook Pro (Retina, 13-inch, Late 2013)
- Localhost or remote server? Localhost
- Database
- MongoDB version: 3.2.1
- Storage engine: wiredTiger
- Hardware: Local - same as above
- Localhost or remote server? Local
Logs/Trace
You can turn on additional logging by configuring VERBOSE=1 in your environment.
REQUEST for [POST] /1/classes/Photo: {
"original": {
"name": "2c1da26912b35d807201efd6300243f0_post.jpeg",
"__type": "File"
},
"color": "black"
}
error: Error generating response. [Error: Tried to encode an unsaved file.]
error: Uncaught internal server error. [Error: Tried to encode an unsaved file.] Error: Tried to encode an unsaved file.
at encode (/Users/tzabian/Projects/parse-server/node_modules/parse-server/node_modules/parse/lib/node/encode.js:63:13)
at exports.default (/Users/tzabian/Projects/parse-server/node_modules/parse-server/node_modules/parse/lib/node/encode.js:95:10)
at ParseObjectSubclass.toJSON (/Users/tzabian/Projects/parse-server/node_modules/parse-server/node_modules/parse/lib/node/ParseObject.js:456:48)
This seems to be an issue with File objects not returning the url in before/afterSave triggers. @nlutsenko - I've currently 'hijacked' your commit to solve this while I continue working on fixing other issues. Would be good to get someone to help with this as I have little knowledge of the underlying parse-server code. Here's what I've done in order to patch this locally.
in RestWrite.js
RestWrite.prototype.expandFilesForExistingObjects = function () {
// Check whether we have a short-circuited response - only then run expansion.
if (this.response && this.response.response) {
this.config.filesController.expandFilesInObject(this.config, this.response.response);
} else if (this.data) {
this.config.filesController.expandFilesInObject(this.config, this.data);
}
};
I can confirm this problem. Since we've moved to the self-hosted parse-server we're experiencing this bug when we're creating a new ParseObject with an associated ParseFile through the REST API. We are using the S3Adapter for our ParseFiles. Removing the beforeSave fixes the issue.
Our steps to reproduce:
-
Have a
beforeSavefor the classGameScorethat does nothing but aresponse.success() -
Upload a new random file:
- Request
curl --request POST \ --url https://<yourURL>/parse/files/Testfile.jpg \ --header 'content-type: image/jpeg' \ --header 'x-parse-application-id: <yourAppId>'- Result:
{ "url": "https://<yourS3Bucket>.s3.amazonaws.com/1d683c3cda5fdb3b8b3e4cbf52612b39_Testfile.jpg", "name": "1d683c3cda5fdb3b8b3e4cbf52612b39_Testfile.jpg" } -
Create a new
GameScorerecord with an association to the uploadedParseFile- Request
curl --request POST \ --url https://<yourURL>/parse/classes/GameScore \ --header 'content-type: application/json' \ --header 'x-parse-application-id: <yourAppId>' \ --data '{"testFile": {"__type": "File","name": "1d683c3cda5fdb3b8b3e4cbf52612b39_Testfile.jpg"}}'- Result:
{ "code": 1, "message": "Internal server error." }
Actual Outcome
The ParseObject will not be saved and there is the following error message in the logs:
error: Uncaught internal server error. [Error: Tried to encode an unsaved file.] Error: Tried to encode an unsaved file.
at encode (/var/app/current/node_modules/parse-server/node_modules/parse/lib/node/encode.js:63:13)
at exports.default (/var/app/current/node_modules/parse-server/node_modules/parse/lib/node/encode.js:95:10)
at ParseObjectSubclass.toJSON (/var/app/current/node_modules/parse-server/node_modules/parse/lib/node/ParseObject.js:456:48)
at /var/app/current/node_modules/parse-server/lib/triggers.js:240:64
at new Promise (/var/app/current/node_modules/parse-server/node_modules/babel-polyfill/node_modules/core-js/modules/es6.promise.js:191:7)
at Object.maybeRunTrigger (/var/app/current/node_modules/parse-server/lib/triggers.js:224:10)
at /var/app/current/node_modules/parse-server/lib/RestWrite.js:187:21
at run (/var/app/current/node_modules/parse-server/node_modules/babel-polyfill/node_modules/core-js/modules/es6.promise.js:87:22)
at /var/app/current/node_modules/parse-server/node_modules/babel-polyfill/node_modules/core-js/modules/es6.promise.js:100:28
at flush (/var/app/current/node_modules/parse-server/node_modules/babel-polyfill/node_modules/core-js/modules/_microtask.js:18:9)
Environment Setup
- Server
- parse-server version: 2.2.17
- Operating System: Amazon Linux
- Hardware: EC2 instance type m4.large
- Localhost or remote server? (AWS, Heroku, Azure, Digital Ocean, etc): AWS Elastic Beanstalk
Any updates on this issue? I think this is a major problem for projects relying on beforeSave triggers...
@nlutsenko any feedback?
Ok found the solution. As long as you are uploading the file right before creating the new object with the association to the file you just have to add the url from the file upload response to your request:
curl --request POST \
--url https://<yourURL>/parse/classes/GameScore \
--header 'content-type: application/json' \
--header 'x-parse-application-id: <yourAppId>' \
--data '{"testFile": {"__type": "File","name": "1d683c3cda5fdb3b8b3e4cbf52612b39_Testfile.jpg", "url": "https://<yourS3Bucket>.s3.amazonaws.com/1d683c3cda5fdb3b8b3e4cbf52612b39_Testfile.jpg"}}'
It would be a good idea to update the part "Associating with Objects" in the REST API Docs :)
Can we close this?
Docs PRs are also welcome! They're at https://github.com/ParsePlatform/Docs