-1

Can anyone figure out why I get

TypeError: getStatusCode(...) is not a function

when I do?

const getStatusCode = require('./getStatusCode')
tmpStatus = await getStatusCode({url: url, timeOut: to, maxRedirects: mr})
(tmpStatus === alert['Check']['Status_code'] ) ? isOk = 1 : isOk = 0

The problem goes any if I remove the last line, where I check the value in tmpStatus.

getStatusCode.js

const axios = require('axios')
const qs = require('qs')

module.exports = async function(options) => {
  options              = options || {}
  options.url          = options.url || {}
  options.string       = options.string || null
  options.timeOut      = options.timeOut || 1000
  options.maxRedirects = options.maxRedirects || 0

  try {
    const response = await axios.get(options.url, {
      timeout: options.timeout,
      maxRedirects: options.maxRedirects,
      validateStatus: null,
      transformResponse: [function (data) {
        return data.search(options.string)
  }],
    })
    return await response.data
  } catch (error) {
    return -1
  }
}
Sandra Schlichting
  • 25,050
  • 33
  • 110
  • 162

2 Answers2

4

The error is because you didn't put a ; at the end of the assignment line. So it's being interpreted as if you wrote

tmpStatus = await getStatusCode({url: url, timeOut: to, maxRedirects: mr})(tmpStatus === alert['Check']['Status_code'] ) 
    ? isOk = 1 : isOk = 0;

This is trying to use the result of getStatusCode() as a function, with the value of tmpStatus === alert['Check']['Status_code'] as its argument.

See What are the rules for JavaScript's automatic semicolon insertion (ASI)?

I recommend you always use semicolons, rather than depending on ASI.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • I had no idea that a function could return a function! When I Google `nodejs return function from function` I don't get anything meaningful. Can you give me a link where I can read about this? – Sandra Schlichting Aug 28 '20 at 09:17
  • 1
    https://davidwalsh.name/javascript-functions I found many answers by googling `javascript function returning function` – Barmar Aug 28 '20 at 09:19
  • `require()` is a function, and it's returning a function when you do `getStatusCode = require(...)`. So how could you be unaware that a function can return a function? – Barmar Aug 28 '20 at 09:21
1

Try removing the =>.

const axios = require('axios')
const qs = require('qs')

module.exports = async function(options) {
  options              = options || {}
  options.url          = options.url || {}
  options.string       = options.string || null
  options.timeOut      = options.timeOut || 1000
  options.maxRedirects = options.maxRedirects || 0

  try {
    const response = await axios.get(options.url, {
      timeout: options.timeout,
      maxRedirects: options.maxRedirects,
      validateStatus: null,
      transformResponse: [function (data) {
        return data.search(options.string)
  }],
    })
    return await response.data
  } catch (error) {
    return -1
  }
}
elpmid
  • 872
  • 1
  • 6
  • 18