Scheduled Job unable to complete post request

(I am completely new to the world of JavaScript, node.js, and Heroku so I apologize in advance if my question is not very clear) I recently set up a Heroku node.js application with a scheduled task that should run every hour.

The task does run as expected and the fireHook() method gets called (I can verify this from the console logs), but for some reason the request itself does not seem to be working (no logs or errors are observed).

This is the code snippet in question:

#!/usr/bin/env node

var request = require('request')


function fireHook(){
 console.log("firing")
 request({
    url: 'https://XXXXXX.herokuapp.com/jobs',
    method: "POST",
    headers: { "content-type": "application/json" },
    json: {"user_id":{ "id":"ddddd"}}

}, function(error, response, body) {
    console.log(response)
    if (error) {
        console.log('Error sending messages: ', error)
    } else if (response.body.error) {
        console.log('Error: ', response.body.error)
    } else {
        console.log('success: ', body)
        reportToSnitch()
    }
})
} 
fireHook();
process.exit();

I have 2 questions:

  1. Why isn't the request functioning properly?
  2. Currently, I'm using a webhook to communicate with my main app. Is there a more efficient way to directly call a function on the main app from within the script?

Appreciate any insights you can provide!

Answer №1

Your process.exit() might be prematurely terminating your script, causing the request to not receive a response in time. To resolve this issue, try moving it inside the callback function:

#!/usr/bin/env node

var request = require('request')


function fireHook(){
 console.log("firing")
 request({
    url: 'https://XXXXXX.herokuapp.com/jobs',
    method: "POST",
    headers: { "content-type": "application/json" },
    json: {"user_id":{ "id":"ddddd"}}

}, function(error, response, body) {
    console.log(response)
    if (error) {
        console.log('Error sending messages: ', error)
    } else if (response.body.error) {
        console.log('Error: ', response.body.error)
    } else {
        console.log('success: ', body)
        reportToSnitch()
    }
    process.exit();
})
} 
fireHook();

Remember that JavaScript is asynchronous, so timing is crucial when dealing with events. If reportToSnitch() is also an asynchronous function, you may need to call process.exit() at the end of that function for proper execution.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Incorporating text sections into a div container and adjusting the width

Currently facing an issue with the canvas element on my project. <div id="app-container"> <div id="canvas-container"> <div id="canvas"></div> </div> </div> In the CSS stylesheet, the following styles ar ...

I encountered a 404 (Not Found) error when attempting to access http://localhost:3000/. Interestingly, I intended to make a

const express = require('express') const app = express() const port = 3000 app.post('/', (req, res) => { res.send('Hello World!') }) // app.post('/post', (req, res) => { // res.send('Hello World!&apo ...

Having trouble uploading Node.js and Mongoose to Heroku due to error codes H12 and H15? Need help troubleshooting and resolving this issue?

Attempting to deploy my Node, mongoose, express app on Heroku for the first time has been a challenge. The application is a simple blog with a login system. Despite extensive research and effort, I am struggling to successfully host it. Below is the error ...

Is there a way to make a button on a single div only affect that specific div

I have a PHP query that echoes a div for each row in the table. I want the div to slide up and then, when the user clicks the "read more" button, the div slides down. However, since it is echoed in a loop, all the divs have the same IDs and classes. I wo ...

Tips for preventing the inheritance of .css styles in React

I'm facing an issue with my react application. The App.js fragment is displayed below: import ServiceManual from './components/pages/ServiceManual' import './App.css'; const App = () => { return ( <> ...

I am experiencing difficulty retrieving a specific item from my Sqlite database using the Express application

I'm in the process of developing an application where users have the ability to create posts and interact by commenting on them. The functionality for creating, updating, and deleting posts is working properly, as well as creating comments. When a us ...

Tips on how to render a component only after receiving an AJAX response

I'm encountering an issue with one of my React components. It seems like AJAX is not fetching all the content from the external server before React renders the ChildComp component. https://i.stack.imgur.com/o0ZtH.png Above, you can view the tree of ...

Having trouble deciphering this snippet of Express JS source code

Upon reviewing the Express JS source code, I came across the main module where express is being exported. module.exports = createApplication; function createApplication() { var app = function(req, res, next) { app.handle(req, res, next); }; m ...

Uncovering the Issue with Select All Functionality in <Table/> when using Material-UI and React

When using Material-UI's <Table/> with ReactJS, a table is set up with a select all checkbox. Each time an individual row checkbox is clicked, the row id is added to the state array clickedRowIds. This allows for logging of the ids of the clicke ...

Is there a universal method to close all pop-up modals and cookie notifications with Puppeteer?

Is there a universal method to dismiss cookie warnings and modals when capturing a screenshot with puppeteer? ...

sophisticated method for sorting through data within an array of arrays

How can data be filtered from an array of arrays? Below is an example to help explain the process. To filter the data, use startnumber and endnumber in the query. const data = [ { "name": "x", "points": [ [100, 50, 1], //[number, value ...

Displaying Vue.js tooltips in a table when the text gets clipped

I'm currently facing an issue with creating a tooltip in my vue.js document. I attempted to follow this guide from https://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_tooltip in order to create one, but it's not working as expected. Her ...

Utilizing MongoDB Data in HBS Template Rendering

Attempting to showcase data from my mongodb backend on an hbs template by following this tutorial. No errors are shown in server logs or browser console, but no data is visible on the template. Interestingly, it generates HTML markup. Seeking assistance to ...

Transfer the file image stored in a MySQL database to an excel spreadsheet

Having an issue with capturing image data input from a PHP and HTML database to Excel. I am trying to display the images in a specific size within an Excel file. Below is the PHP code used to create the table: <?php header("Content-type: application/o ...

Tips for navigating through pagination indexes with Angular 6

Hey there, I'm currently working on a project where I need to automatically click through each pagination index. Initially, only the first index can be clicked. After that, the following page indexes are not clickable. Here's the code snippet: ...

What is the method for adding line breaks to a JSON file?

I've been developing a Discord bot and I'm currently storing currency values in a json file. The functionality is working smoothly, but the issue I'm facing is that it's adding them to the json file in a single line which makes it diffi ...

What is the process for transforming information from a URL/API into a table in a node.js express application with jade?

I have attempted the following code in my index.js file: var express = require('express'); var router = express.Router(); var request = require('request'); /* GET home page. */ router.get('/', function(req, res, next) { r ...

I need help using i18N to translate the SELECT option in my VUE3 project. Can someone guide me

<n-select v-model:value="value" :options="options" /> options: [ { label: "Every Person", value: 'file', }, { label: 'Drive My Vehicle', ...

Customize the appearance of Woocommerce's blockUi feature with your

During an Ajax request, blockUI adds a style to the blocks of the checkout form and cart with "background: '#fff'". However, my entire website theme is black and I do not want the background color of the blocks to be white. Is there a way to remo ...

How big is the array size in the WebAudio API data?

Exploring the visualization of waveform and FFT generated by the audio stream from the microphone through the WebAudio API. Curiosity strikes - what is the size of each data array available at a given moment? Delving into the getByteTimeDomainData, it men ...