I'm attempting to transfer my Google Sheets data to MongoDB Atlas. I've created the app script and function in the editor. However, when running the code in MongoDB's function editor, I encounter the ERROR "mongodb insert: argument must be an object". Here is an overview of my progress:
Google Sheet APP Script
function exportSheetsToMongo () {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spreadsheet.getSheetByName("US");
var headersRows = 1;
var range = sheet.getDataRange();
var numsRows = range.getNumRows;
var data = range.getValues();
for (var i = headersRows; i < numsRows; i++) {
var usZipIdCell = range.getCell( i +1, columns.zip+1) // Error possible here
// var description = data[i][columns.description];
var formData = {
// 'country': data[i][columns.description],
'zip': data[i][columns.zip],
'city': data[i][columns.city],
'state': data[i][columns.state],
'state_code': data[i][columns.state_code],
'county': data[i][columns.county],
'county_code': data[i][columns.county_code],
'lattitude': data[i][columns.lattitude],
'longitude': data[i][columns.longitude],
'accuracy': data[i][columns.accurcay]
};
var options = {
'method':'post',
'payload': formData
};
if(description) {
var insertID = UrlFetchApp.fetch("https://us-east-1.aws.data.mongodb-api.com/app/mmc-backend-mzbhi/endpoint", options);
usZipIdCell.setValue(insertID)
}
}
}
MongoDB Realm Function Editor
exports = async function(payload) {
const mongodb = context.services.get("mongodb-atlas");
const eventsdb = mongodb.db("us_zip");
const eventscoll = eventsdb.collection("zip");
const result = await eventscoll.insertOne(payload.query);
var id = result.insertedId.toString();
if(result) {
return JSON.stringify(id,false,false);
}
return {text: 'Error saving'}
};
Partial Google Sheet Data
https://i.sstatic.net/FHMe3.png
Data Base