-
-
Save gaodeng/54b30261f7fee9ebd24a00744b2f2963 to your computer and use it in GitHub Desktop.
Example of importing data with cloud functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| curl -X POST \ | |
| -H "X-Parse-Application-Id: LL9oIdzIkmwl5xyowQQu0fTmXyUWfet9RuAzwHfj" \ | |
| -H "X-Parse-REST-API-Key: R3S8PYQKuzeV4c8MUeO5ved46C50MEp56boDHW1O" \ | |
| -H "Content-Type: application/json" \ | |
| -d @data.json \ | |
| https://parseapi.back4app.com/functions/import |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "className": "ExampleClass", | |
| "rows": [ | |
| { "ExampleColumnA": "row1columnA", "ExampleColumnB": "row1columnB" }, | |
| { "ExampleColumnA": "row2columnA", "ExampleColumnB": "row2columnB"} | |
| ] | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Parse.Cloud.define("import", function (request, response) { | |
| var className = request.params.className; | |
| var rows = request.params.rows; | |
| var MyClass = Parse.Object.extend(className); | |
| var promises = []; | |
| for (var i = 0; i < rows.length; i++) { | |
| var myClassObject = new MyClass(); | |
| for (var column in rows[i]) { | |
| myClassObject.set(column, rows[i][column]); | |
| } | |
| promises.push(myClassObject.save()); | |
| } | |
| Parse.Promise | |
| .when(promises) | |
| .then( | |
| function () { | |
| response.success('Successfully imported ' + i + ' rows into ' + className + ' class'); | |
| }, | |
| function (error) { | |
| response.error('Import failed: ' + error); | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment