【问题标题】:TypeError: "callback" argument must be a functionTypeError:“回调”参数必须是函数
【发布时间】:2017-05-08 15:18:34
【问题描述】:

所以我正在按照指南使用 twit 将图像上传到 twitter w 节点。

这是我的代码

    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!');
          }
        }
      );
    }
  });
}

也许我在回调函数中遗漏了一些东西,我知道是否必须是一个函数,但我不明白为什么我的 func 不起作用。

错误:

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

完整代码:

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
);

完全错误:

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

【问题讨论】:

  • 您需要通过删除感叹号来修复if (!err) { 的两行:if (err) { 如果这样做,代码应该可以工作;它对我有用。我也有点困惑,因为你提到的错误看起来不像是一条错误消息,而更像是一个应该抛出一个的脚本行;你到底是从哪里得到这个的?控制台...?
  • 我已经这样做了。仍然是同样的错误。是的,来自控制台。
  • 您能否发布您在控制台中收到的确切错误消息?全文?
  • @ChrisG 是的,给我一点时间。
  • upload_random_image(), 去掉括号就可以了

标签: javascript node.js express twitter callback


【解决方案1】:

您声明 setInterval() 不正确,正确的方法是

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);

【讨论】:

    【解决方案2】:

    背景

    setIntervalexpects a function作为第一个参数。

    在您的代码中

    setInterval(upload_random_image(), 10000);
    

    upload_random_image 正在被调用并且什么也不返回。

    解决方案

    就像 Karthikeyan 提到的,为了仍然调用 upload_random_image,最好将它包装在一个函数中:

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

    【讨论】:

      【解决方案3】:

      按照 sophia.onion 的建议,不要将您的函数包装在另一个匿名函数中。只需删除括号,以便您传递函数引用而不是执行的函数。

      问题:这会执行函数 upload_random_image 而不是将其作为参数传递。

      setInterval(upload_random_image(), 10000);
      

      解决方案:传递函数引用而不是执行。

      setInterval(upload_random_image, 10000);
      

      【讨论】:

        猜你喜欢
        • 2018-10-30
        • 2020-04-16
        • 2021-05-02
        • 1970-01-01
        • 2019-01-24
        • 2018-08-11
        • 2020-06-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多