Hi,
I’m confused by the way two HTTP services are behaving differently.
The first service is based on the tutorial at GitHub - mrlynn/stitching-sheets: Stitching Sheets: Using MongoDB Stitch to Create an API for Google Sheets and has a hook based on stitching-sheets/source.js at master · mrlynn/stitching-sheets · GitHub
exports = async function(payload) {
console.log('here is payload query object ' + JSON.stringify(payload.query));
const mongodb = context.services.get("mongodb-atlas");
const eventsdb = mongodb.db(“my”db);
const eventscoll = eventsdb.collection(“my collection");
//create values for the payload items sent to the endpoint
const user = payload.query.user;
const userName = payload.query.userName;
const year = payload.query.year.substr(0, 4);
const player = payload.query.player;
const manufacturer = payload.query.manufacturer;
const brand = payload.query.brand;
const series = payload.query.series;
const grader = payload.query.grader;
const graded_id = Number(payload.query.graded_id);
const career_stage = payload.query.career_stage;
const team = payload.query.team;
const card_number = Number(payload.query.card_number);
const print_run = Number(payload.query.print_run);
const number = Number(payload.query.number);
const image_path = payload.query.image_path;
const forTrade = payload.query.forTrade;
const status = payload.query.status;
const purchase_date = payload.query.purchase_date;
const card_id = payload.query.card_id;
const result= await eventscoll.updateOne({ card_id: card_id },
{ $set: {
user:user,
userName:userName,
year: year,
player:player,
manufacturer:manufacturer,
brand : brand,
series : series,
grader : grader,
graded_id : graded_id,
career_stage : career_stage,
team : team,
card_number : card_number,
print_run : print_run,
number : number,
image_path : image_path,
status : status,
forTrade:forTrade,
purchase_date : purchase_date
}
},
{ upsert: true }
);
console.log('here is payload object ' + JSON.stringify(payload.query));
if(result) {
console.log('here is result ' + JSON.stringify(result));
//return JSON.stringify(id,false,false);
}
return { text: `Error saving` };
}
From Google App Script I POST to this via
var options = {
method: "post",
payload: formData, //a JSON representation of the cells in the Google Sheet
};
if (player) {
var insertID = UrlFetchApp.fetch(
"https://webhooks.mongodb-realm.com/api/client/v2.0/app/cards-fvyrn/service/AddCards/incoming_webhook/Aards",
options
);
This all works fine from Google Sheets. However I want to make POST request to the same endpoint via Axios or POSTMAN but I get an error
‘“error”: “{"message":"Cannot access member ‘substr’ of undefined"}”,
“error_code”: “FunctionExecutionError”,
‘
I’ve created another test POST endpoint with the code
exports = async function (payload, response) {
//console.log("payload.query "+ JSON.stringify(payload.query));
console.log("payload.body"+JSON.stringify(payload.body));
const query = EJSON.parse(payload.body.text())
console.log("payload.query "+ JSON.stringify(query));
const results = await context.services
.get("mongodb-atlas")
.db(“my db)
.collection("my collection")
.insertOne( query )
response.setBody(results ? JSON.stringify(results) : "{}");
}
I can send the same JSON object to this endpoint and it is submitted to the collection fine. I’m confused about why in this second function I am not able to access any properties of payload.query (e.g. payload.query.career_stage from the first example) but instead have to access the query values via
const query = EJSON.parse(payload.body.text()).
Also, can anyone suggest how I could amend this service to accept an array of objects, so that where the document IDs match the document is updated, and where there is no match a new document is created? Sorry if this last question should be a separate question