Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save flutter-painter/192f8fca182d904717e06ec12d75a8d7 to your computer and use it in GitHub Desktop.
Save flutter-painter/192f8fca182d904717e06ec12d75a8d7 to your computer and use it in GitHub Desktop.
stitch webhook example to import data from spreadsheet
exports = async function(payload) {
const mongodb = context.services.get("mongodb-atlas");
//const {flavor} = payload.query;
const flavor = 'test';
const myDB = mongodb.db(`YOURDB`);//TODO: replace
const myCollection = myDB.collection(`myCollection`); //TODO: replace
const body = payload.body.text();
const reqBody = JSON.parse(body);
console.log("Request body:", reqBody);
console.log(body);
const myLastObject = await myCollection.find().sort({_id:-1}).limit(1); // TODO replace
console.log(JSON.stringify(myLastObject));
//var counter = 0;
//if(!myLastObject.id) {
// console.log("entering condition");
// counter = myLastObject.id;
// console.log(counter);
//}
//console.log(JSON.stringify(myLastObject.id));
const results = [];
for(var i = 0; i < reqBody.length; i++) {
var query = reqBody[i];
console.log(JSON.stringify(query));
var d = Date.now();
//var d = date.toISOString();
const myObject = { // toDO
id: new BSON.Int32(query.id),
property1: query.property_1,
property1: query.property_2,
const result= await myCollection.insertOne(ticket);
var id = result.insertedId.toString();
results.push(id);
//counter+= 1;
}
console.log(results);
//return { text: `Error saving` };
return JSON.stringify(results);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment