Reputation: 1041
I want to call one API over each row of google sheet. API will return the JSON object which I want to push in the array after all rows completed. I want to this array for further process.
for e.g resultArr = [ {first Api result}, {second api result} .... so on]
below one is my code which I have a try
const axios = require("axios");
const assert = require("assert");
const path = require("path");
const fs = require("fs");
const { GoogleSpreadsheet } = require("google-spreadsheet");
const doc = new GoogleSpreadsheet("ABCDEFGHIJKLMOPQRSTUVWxyz");
doc.useApiKey("ABCDEFGHIJKLMOPQRSTUVWxyz");
let jsonRowArr = ["initial"];
async function test1() {
await doc.loadInfo();
const sheet = doc.sheetsByIndex[0]; /* or use doc.sheetsById[id] */
/* read all rows */
const rows = await sheet.getRows(); // can pass in { limit, offset }
rows.forEach(async (row, index) => {
let heading = row["_sheet"]["headerValues"];
fieldValue = {};
let rowValue = row["_rawData"];
heading.forEach((key, i) => {
fieldValue[key] = rowValue[i];
});
//start here
let APIParam = {
test_name: `test ${index}`,
shipping_method: {
before_decimal_point: fieldValue["Before Decimal"],
after_decimal_point: fieldValue["After Decimal"],
closest_round: "100",
},
shipping_price: fieldValue["3 Digit"],
method_name: "get_shipping_after_round",
expected_rate_up: ["Expected Down 3"],
expected_rate_down: ["Expected Up"],
};
APIAfterApiData = await apiCallNewFn(APIParam);
await jsonRowArr.push("dsv");
await jsonRowArr.push(APIAfterApiData);
});
return await jsonRowArr;
}
apiCallNewFn = async (params) => {
let { data } = await axios.post(
"http://api.example.com/call_function.php",
null,
{
params,
}
);
// console.log('data...', data);
data["test_name"] = await params.test_name;
data["expected_rate_up"] = await params.expected_rate_up;
data["expected_rate_down"] = await params.expected_rate_down;
return await data;
};
(async () => {
let customTestObj = await test1();
console.log("I want fianl result here");
console.log(customTestObj);
console.log("for further process");
})();
Upvotes: 0
Views: 313
Reputation: 201368
How about this modification? In this modification, I used that apiCallNewFn
returns Promise. And the for loop is used instead of forEach. Ref When your script is modified, it becomes as follows. In this modification, test1()
and apiCallNewFn
are modified.
test1()
:async function test1() {
await doc.loadInfo();
const sheet = doc.sheetsByIndex[0];
const rows = await sheet.getRows();
for (let index = 0; index < rows.length; index++) {
const row = rows[index];
let heading = row["_sheet"]["headerValues"];
fieldValue = {};
let rowValue = row["_rawData"];
heading.forEach((key, i) => {
fieldValue[key] = rowValue[i];
});
let APIParam = {
test_name: `test ${index}`,
shipping_method: {
before_decimal_point: fieldValue["Before Decimal"],
after_decimal_point: fieldValue["After Decimal"],
closest_round: "100",
},
shipping_price: fieldValue["3 Digit"],
method_name: "get_shipping_after_round",
expected_rate_up: ["Expected Down 3"],
expected_rate_down: ["Expected Up"],
};
APIAfterApiData = await apiCallNewFn(APIParam);
jsonRowArr.push("dsv");
jsonRowArr.push(APIAfterApiData);
}
return jsonRowArr;
}
apiCallNewFn()
:const apiCallNewFn = async (params) => {
return axios
.post(
"http://api.example.com/call_function.php",
null,
{
params,
}
)
.then((res) => {
let data = { res: res.data };
data["test_name"] = params.test_name;
data["expected_rate_up"] = params.expected_rate_up;
data["expected_rate_down"] = params.expected_rate_down;
return data;
});
};
or
const apiCallNewFn = async (params) => {
return axios
.post(
"http://api.example.com/call_function.php",
null,
{
params,
}
)
.then((data) => {
data["test_name"] = params.test_name;
data["expected_rate_up"] = params.expected_rate_up;
data["expected_rate_down"] = params.expected_rate_down;
return data;
});
};
apiCallNewFn
, please modify the script for the result you expect.Upvotes: 1