InsertMany with webhook Realm

Hi,

I am trying to insert many (insertMany) records with realm webhook and google sheet using google app script into MongoDB.
but unfortunately getting an error

Exception: Attribute: payload[0], provided with invalid value: {Vehicle__MAKE=Acura, Vehicle__YEAR=2003

Here is the Google app script code.

for (var i=headerRows; i<numRows; i++) {
    var formData = {
      Vehicle__YEAR: `${data[i][columns.Vehicle__YEAR]}`,
      Vehicle__MAKE: `${data[i][columns.Vehicle__MAKE]}`,
      
    }
    batchrecords.push(formData);
    if(i == 5)break;
  }
  Logger.log(batchrecords)
  var options = {
     'method' : 'post',
     'payload' : batchrecords,
  };
  var insertID = UrlFetchApp.fetch('https://us-east-3.aws.webhooks.mongodb-realm.com/api/client/v2.0/app/googlesheet-xyisa/service/expoor/incoming_webhook/insert', options);

Here is the MongoDb Realm Sttich webhook code.

exports = async function(payload) {
 const mongodb = context.services.get("mongodb-atlas");
 const eventsdb = mongodb.db("Test");
 const eventscoll = eventsdb.collection("cars");
 const result= await eventscoll.insertMany(payload.query);
 if(result) {
 return { text: `Inserted` };
 }
 return { text: `Error saving` };
}

Here is the screenshot of logged data in the app script.
logger_app_script_data

getting error

Exception: Attribute: payload[0], provided with invalid value: {Vehicle__MAKE=Acura, Vehicle__YEAR=2003

Can anyone help me in this regard?
Thanks

Hi,

I solve this by using this
Note (must console data in realm function which type of data you are receiving from request)

First, stringify the data

const payload = {
      "data": arr
}

var options = {
     'method' : 'POST',
     'payload': JSON.stringify(payload),
};

send the request to the MongoDB server webhook realm
and Then parse data in realm function.

const result= await eventscoll.insertMany(JSON.parse(Object.keys(payload.query)).data);

Finally, the problem is sovled.
Thanks

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.