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
                The "callback is not a function" error occurs when we define a callback parameter to a function, but invoke the function without passing a callback as a parameter. To solve the error, provide a function as a default parameter, or always provide a parameter when calling the function.
A callback is a function passed as an argument to another function. This technique allows a function to call another function. A callback function can run after another function has finished.
This is a standard JavaScript error when trying to call a function before it is defined. This error occurs if you try to execute a function that is not initialized or is not initialized correctly. This means that the expression did not return a function object.
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)
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With