【发布时间】: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