-
-
Save grosscorporation/d2c873d8fada3077fb58db8faefe6024 to your computer and use it in GitHub Desktop.
Example of importing data with cloud functions
This file contains 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 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); | |
}); | |
}); |
This file contains 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment