0

I am learning backend development can you help me to hit an api? I tried doing something like this but it's not hitting the API also it's not logging "Making API call" I tried to console log expirationDate, token, and email and they three logs correctly but still it's not hitting the API and not logging Making API call What's wrong with my code and how can I fix it?

Also No error In the console

Also I am using node-schedule and the value of expirationDate, token, and email look like this

2023-03-30T01:48:46.962Z dsvicPH1SSOCdHWpxrMrvF:APA91bFYXRKOsa2F_JTYkEOLiPM32o1NoWDi2fgE_vssum3csYJt_kLRZK9BfARoogQ7Q3xxWeFREo3J4ms_MEZYPGo_bEWlVZzG-kEJC_juw8fvw8J3OXiJNE4w0owjiAuIHWOcRhsR idsdsdd@gmail.com

The Token is a FCM token What is FCM token in Firebase?

function scheduleApiCall(expirationDate, token, email) {
    console.log(expirationDate, token, email)
    // Schedule the API call to be made at the specified date and time
    const job = schedule.scheduleJob(expirationDate, async function () {
        // This function will run at the specified date and time
        // Make the API call here
        console.log('Making API call');
        try {
            const response = await axios.post(
                'http://localhost:3000/getnoti',
                {
                    title: 'SERVER NOTIFICATION',
                    body: 'I am coming from server',
                    token: token
                },
                {
                    headers: {
                        'Content-Type': 'application/json'
                    }
                }
            );
            console.log(response);
        } catch (error) {
            console.error(error);
        }
    });

    // job.cancel();
}
Hack
  • 3
  • 2

1 Answers1

0

Reading node-schedule documentation, you're not passing expiration date as the first argument, but just date (or schedule). Copying-pasting from the docs:

Say you very specifically want a function to execute at 5:30am on December 21, 2012. Remember - in JavaScript - 0 - January, 11 - December.

const schedule = require('node-schedule');
const date = new Date(2012, 11, 21, 5, 30, 0);

const job = schedule.scheduleJob(date, function(){
  console.log('The world is going to end today.');
});

Basically, you scheduled the job "do it exactly at 2023-03-30T01:48:46.962Z" (which, as of now, is a date several hours in the future).

https://github.com/node-schedule/node-schedule#readme

mbojko
  • 13,503
  • 1
  • 16
  • 26