2
I intend to perform an Insert in a table in Postgresql.
The values that will be used in the query are from a JSON in which I have access through a GET.
fetch(
"https://api.fitbit.com/1/user/-/activities/steps/date/2021-03-23/today.json",
{
method: "GET",
headers: { Authorization: "Bearer " + access_token },
}
).then((value) => value.json())
.then((json) => {
console.log(json);
The JSON file from the above code:
'activities-steps': [
{ dateTime: '2021-03-23', value: '3463' },
{ dateTime: '2021-03-24', value: '449' },
{ dateTime: '2021-03-25', value: '4794' },
{ dateTime: '2021-03-26', value: '4992' },
{ dateTime: '2021-03-27', value: '489' },
{ dateTime: '2021-03-28', value: '781' },
{ dateTime: '2021-03-29', value: '706' }
The connection to the bank works perfectly and I already have Function for the Insert:
async function insertSetps(stepstable) {
const client = await connect();
const sql = "INSERT INTO steps (date,value) VALUES ($1,$2);";
const values = [stepstable.date, stepstable.value];
return await client.query(sql, values);
}
module.exports = { insertSetps };
How to pass the above JSON values inside the insertSetps function below?
(async () => {
const db = require("./db.js");
console.log("Início do insert");
const result = await db.insertSetps({date: VALORES, value: VALORES});
console.log(result.rowCount);
})();
Could someone help me? Thank you.
thank you! The solution was this way. I did a for to go through the entire JSON string and managed to take each value and insert it into the query.
– Matheus Rohde