2016-12-22 85 views
0

因此,我正在按照twit將圖像上傳到twitter w節點。TypeError:「回調」參數必須是一個函數

這是我的代碼

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 
+0

你需要通過刪除感嘆號來解決兩行說'如果(ERR!){':'如果(ERR){'如果你這樣做,該代碼應工作;它爲我做了。我也有點困惑,因爲你提到的錯誤看起來不像一個錯誤信息,更像是一個應該拋出一個的腳本行;你從哪裏得到這個?控制檯...? –

+0

我已經做到了。還有同樣的錯誤。是的,從控制檯。 – Ghamtre

+0

你能在控制檯上發佈確切的錯誤信息嗎?全文? –

回答

4

您在聲明的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); 
相關問題