Hey folks,
I've been building a server that fetches all bounces from Sendgrid. Currently, I'm trying to query for certain results, e.g.
My program currently takes the input date from the client; then, the data is sent to the node.js server, where the server converts the ISO date into a UNIX timestamp. After that, the server fetches the bounce data between the dates provided by the client
Note: I haven't refactored my code just yet.
HTML Code taking date input:
JavaScript that grabs the ISO dates from the client and converts it into UNIX Timestamp
The code used to fetch data from Sendgrid using query parameters:
The results:
Here are the results from the code above; the dates I used are March 1st, 2023, to March 31st, 2023 (for security purposes, I can't show you all the data). The first image shows the date from the client (converted to UNIX), and then just below "created" shows the result of the first entry, which is March 10th. The second image shows a result from January 31st.
Note: The field created is 1 of 4 fields shown in the result: created, email, reason and status.
My questions:
I've been building a server that fetches all bounces from Sendgrid. Currently, I'm trying to query for certain results, e.g.
start_time
& end_time
values Sendgrid requires. But I noticed that it doesn't always return the requested data. Sometimes it's blank with just [ ] as a result (via postman) or shows them all but not within the timeframe I've included in query params.My program currently takes the input date from the client; then, the data is sent to the node.js server, where the server converts the ISO date into a UNIX timestamp. After that, the server fetches the bounce data between the dates provided by the client
Note: I haven't refactored my code just yet.
HTML Code taking date input:
HTML:
<form action="/find" method="POST">
<input type="date" name="searchDateStart" placeholder="Enter first date">
<input type="date" name="searchDateEnd" placeholder="Enter second date">
<button type="submit">Search</button>
</form>
JavaScript that grabs the ISO dates from the client and converts it into UNIX Timestamp
JavaScript:
// Code awaits for the start and end time from the client
const start_time = await req.body.searchDateStart;
const end_time = await req.body.searchDateEnd;
// Code uses a function to convert the ISO dates into UNIX Timestamps
const startTimeCon = convertISOtoUNIX(start_time);
const endTimeCon = convertISOtoUNIX(end_time);
// Code converts ISO date format in UNIX Timestamp
function convertISOtoUNIX(iso) {
return Math.round(new Date(iso).getTime() / 1000);
}
The code used to fetch data from Sendgrid using query parameters:
JavaScript:
const headers = {
"Accept": "application/json"
};
const queryParams = {
'start_time': startTimeCon,
'end_time': endTimeCon,
};
console.log(queryParams);
const request = {
url: `/v3/suppression/bounces`,
method: 'GET',
headers: headers,
query: queryParams
};
client.request(request)
.then(([response, body]) => {
//console.log(response.statusCode);
console.log(response.body);
})
.catch(error => {
console.error(error);
});
res.send(`We can search for this item on our backend: ${start_time} - ${end_time}`)
The results:
Here are the results from the code above; the dates I used are March 1st, 2023, to March 31st, 2023 (for security purposes, I can't show you all the data). The first image shows the date from the client (converted to UNIX), and then just below "created" shows the result of the first entry, which is March 10th. The second image shows a result from January 31st.
Note: The field created is 1 of 4 fields shown in the result: created, email, reason and status.
My questions:
- Why doesn't SendGrid provide back the data I asked for? I've tried running the dates through postman, but it's the same thing, so I assume the timestamp I'm providing is wrong.
- Should I convert ISO dates to timestamps in the client or the server?
Last edited: