Skip to content

Instantly share code, notes, and snippets.

@mirsahib
Last active December 7, 2021 07:08
Show Gist options
  • Save mirsahib/73e19b72ca4730c8e9d3f81a1360a14d to your computer and use it in GitHub Desktop.
Save mirsahib/73e19b72ca4730c8e9d3f81a1360a14d to your computer and use it in GitHub Desktop.
// This function is the webhook's request handler.
exports = async function(payload,response) {
// mongodb connection setup
const mongodb = context.services.get("mongodb-atlas");
const db = mongodb.db("objectAnnotation");
const annotation = db.collection("annotation");
//parse payload to Js object since mongodb use bson format for data transfer
const body =EJSON.parse(payload.body.text());
// create a new js object with the request body
// you can change request body and data object according to the schema
const data = {
className:body.className,
productName:body.productName,
image:body.image,
}
try{
// insert data to db
const result= await annotation.insertOne(data);
// if insetion is successfull send success message with the saved data
if(result){
response.setBody(JSON.stringify({data:result,message:"saved"}))
}else{
response.setBody(JSON.stringify({message:"not saved"}))
}
return {"data":"saved"}
}catch(err){
return err;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment