• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Node.JS Sendgrid is not returning results with a provided timestamp.

Malcolm

Administrator
Administrator
Staff Team
Code Plus
Company Plus
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. 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.

Screenshot_51.png

Screenshot_52.png

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:
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. 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.

View attachment 2063

View attachment 2064

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?
Please remind me if SendGrid has a limitation when it comes to timeframe range...with that said, might be worth attempting it again with a lesser time frame.. like say, an hour's worth of data for the same day, to see if that has any impact
 
Please remind me if SendGrid has a limitation when it comes to timeframe range...with that said, might be worth attempting it again with a lesser time frame.. like say, an hour's worth of data for the same day, to see if that has any impact
I thought the same, but even providing it with the day of or even on the date that does have events, it still pulls either nothing or random data from a different date.
 

New Threads

Latest posts

Buy us a coffee!

300x250
Top Bottom