TypeError: "callback" argument must be a function

20,732

Solution 1

Background

setInterval expects a function as the first parameter.

In your code

setInterval(upload_random_image(), 10000);

upload_random_image is being invoked and return nothing.

Solution

Like Karthikeyan mentioned, in order to still invoke upload_random_image, the best thing is to wrap it in a function:

setInterval(() => upload_random_image(), 10000)

Solution 2

your are declaring the setInterval() incorrect, the correct way is

setInterval(function(){ upload_random_image() },10000);

var Twit = require('twit')

var fs = require('fs'),
    path = require('path'),
    Twit = require('twit'),
    config = require(path.join(__dirname, 'config.js'));


var T = new Twit(config);

function random_cam(){
  var random_pic = [
    '1.jpg',
    '2.jpg',
    '3.jpg'
  ];
  return random_pic[Math.floor(Math.random() * random_pic.length)];
}


function upload_random_image(){
  console.log('Opening an image...');
  var image_path = path.join(__dirname, '/random_cam/' + random_cam()),
      b64content = fs.readFileSync(image_path, { encoding: 'base64' });

  console.log('Uploading an image...');

  T.post('media/upload', { media_data: b64content }, function (err, data, response) {
    if (err){
      console.log('ERROR');
      console.log(err);
    }
    else{
      console.log('Uploaded an image!');

      T.post('statuses/update', {
        media_ids: new Array(data.media_id_string)
      },
        function(err, data, response) {
          if (err){
            console.log('Error!');
            console.log(err);
          }
          else{
            console.log('Posted an image!');
          }
        }
      );
    }
  });
}

setInterval(function(){
     upload_random_image()
},10000);

Solution 3

Don't wrap your function in another anonymous function per sophia.onion's suggestion. Just remove the brackets so you are passing the function reference and not the executed function.

Problem: this executes the function upload_random_image instead of passing it as an argument.

setInterval(upload_random_image(), 10000);

Solution: pass function reference instead of the execution.

setInterval(upload_random_image, 10000);
Share:
20,732
IDGM5
Author by

IDGM5

Updated on July 12, 2022

Comments

  • IDGM5
    IDGM5 almost 2 years

    So I'm following a guide to upload images to twitter w node using twit.

    This is my code

        function upload_random_image(){
      console.log('Opening an image...');
      var image_path = path.join(__dirname, '/random_cam/' + random_cam()),
          b64content = fs.readFileSync(image_path, { encoding: 'base64' });
    
      console.log('Uploading an image...');
    
      T.post('media/upload', { media_data: b64content }, function (err, data, response) {
        if (!err){
          console.log('ERROR');
          console.log(err);
        }
        else{
          console.log('Uploaded an image!');
    
          T.post('statuses/update', {
            media_ids: new Array(data.media_id_string)
          },
            function(err, data, response) {
              if (!err){
                console.log('Error!');
                console.log(err);
              }
              else{
                console.log('Posted an image!');
              }
            }
          );
        }
      });
    }
    

    Maybe I'm missing something with the callback function, I know if had to be a function but I can't see why my func is not working.

    Error:

    throw new TypeError('"callback" argument must be a function');
    

    Full code:

    var Twit = require('twit')
    
    var fs = require('fs'),
        path = require('path'),
        Twit = require('twit'),
        config = require(path.join(__dirname, 'config.js'));
    
    
    var T = new Twit(config);
    
    function random_cam(){
      var random_pic = [
        '1.jpg',
        '2.jpg',
        '3.jpg'
      ];
      return random_pic[Math.floor(Math.random() * random_pic.length)];
    }
    
    
    function upload_random_image(){
      console.log('Opening an image...');
      var image_path = path.join(__dirname, '/random_cam/' + random_cam()),
          b64content = fs.readFileSync(image_path, { encoding: 'base64' });
    
      console.log('Uploading an image...');
    
      T.post('media/upload', { media_data: b64content }, function (err, data, response) {
        if (err){
          console.log('ERROR');
          console.log(err);
        }
        else{
          console.log('Uploaded an image!');
    
          T.post('statuses/update', {
            media_ids: new Array(data.media_id_string)
          },
            function(err, data, response) {
              if (err){
                console.log('Error!');
                console.log(err);
              }
              else{
                console.log('Posted an image!');
              }
            }
          );
        }
      });
    }
    
    setInterval(
      upload_random_image(),
      10000
    );
    

    Complete error:

    Opening an image...
    Uploading an image...
    timers.js:414
        throw new TypeError('"callback" argument must be a function');
        ^
    
    TypeError: "callback" argument must be a function
        at exports.setInterval (timers.js:414:11)
        at Object.<anonymous> (/Users/imac/test/server.js:72:1)
        at Module._compile (module.js:571:32)
        at Object.Module._extensions..js (module.js:580:10)
        at Module.load (module.js:488:32)
        at tryModuleLoad (module.js:447:12)
        at Function.Module._load (module.js:439:3)
        at Module.runMain (module.js:605:10)
        at run (bootstrap_node.js:420:7)
        at startup (bootstrap_node.js:139:9)
        at bootstrap_node.js:535:3