Diogo Barros
Diogo Barros

Reputation: 17

Function is not defined | Node.js

I have boleto.js

var axios = require('axios');    
function getBoleto (token) {
  if (!token) throw Error('Miss Token!');

  return axios.get(
    '/boletos',
    {
      params: {
        token: 'token...'
      }
    }
  ).then(res => {
    //console.log(res.data);
    return res.data;
  })
  .catch(err => {
    console.log('Error boletos', err);
    })
}        
getBoleto()
  .then(boletos => {
    gravar(boletos, function(err){
        if (err) throw new Error('Error boletos');
    });
  });

And gravar.js

module.exports = function (object, callback) {
// Save DB
...
console.log('Saved successfully');
callback();
}

What I need? Save the return of boleto.js in gravar.js

But brought this message : Reference error: gravar is not defined

Upvotes: 1

Views: 11682

Answers (2)

hci
hci

Reputation: 39

Be careful about error message "function X is not defined". My experience in Chrome was painful, because in realy, the function has always been defined. What occured is that there was an error within the function, but message was hiding it.

Upvotes: 1

hya
hya

Reputation: 1738

In boleto.js you need to add:

var gravar = require('./gravar.js');

If gravar.js is in other directory, provide correct relative path.

Upvotes: 2

Related Questions