Node.js Nodejs:Twit节点API状态已发布,无图像且无错误

Node.js Nodejs:Twit节点API状态已发布,无图像且无错误,node.js,api,file-upload,twitter,Node.js,Api,File Upload,Twitter,我希望有人能帮助我。 我尝试过搜索,但似乎找不到任何遇到过此问题的人 我正忙着学习NodeJS,在路上我决定制作一个twitter机器人来帮助我学习。 我将使用: NodeJS Twit节点API 在我的Win 10桌面上运行 这个Twitter机器人本质上是从一个包含.png图像的文件夹中发布一个随机图像,并以prefs.js文件中存储的两个给定时间间隔从分隔的.txt文件中发布一个随机状态 e、 g.1.png,Hello World,最小间隔时间:400分钟,最大间隔时间:600分钟

我希望有人能帮助我。 我尝试过搜索,但似乎找不到任何遇到过此问题的人

我正忙着学习NodeJS,在路上我决定制作一个twitter机器人来帮助我学习。 我将使用:

  • NodeJS
  • Twit节点API
  • 在我的Win 10桌面上运行
这个Twitter机器人本质上是从一个包含.png图像的文件夹中发布一个随机图像,并以prefs.js文件中存储的两个给定时间间隔从分隔的.txt文件中发布一个随机状态

e、 g.1.png,Hello World,最小间隔时间:400分钟,最大间隔时间:600分钟

这个机器人大部分时间都运行得很完美,但是有时它只是从.txt文件中发布状态,没有图像,也没有错误消息

我最初认为是.png图像大小,并将它们都减小到3Mb以下,并认为问题得到了解决,但在一次运行中又出现了一次失败

我决定找到一个状态被发布而没有图像的实例,以测试特定的图像是否导致了问题。 一旦我找到了一个,我就从“images”文件夹中删除了除失败图像之外的所有其他图像,并对其进行了测试,但它多次上传图像并以无任何问题的状态发布

我似乎不明白为什么会发生这种情况和/或为什么我在失败时没有收到任何错误消息。希望有人能帮我

此外,任何关于改进代码的提示都是欢迎的

我在下面添加了不带间隔的主要功能:

const fs = require("fs"),
  path = require("path"),
  Twit = require("twit"),
  prefs = require("./prefs.js");
config = require(path.join(__dirname, "config.js"));

// Tweet random image along with a random status
function tweetRandomImage() {
  //read filenames from directory into an array
  fs.readdir(__dirname + "/images", function (err, files) {
    if (err) {
      console.log("error:", err);
    } else {
      let images = [];
      files.forEach(function (f) {
        let imagePath = path.join(__dirname, "/images/" + f);
        let filesize = getFilesizeInMegabytes(imagePath);
        let file_ext = path.extname(f);

        images.push(f);
        
        });

      console.log("opening an image...");

      const imagePath = path.join(__dirname, "/images/" + randomFromArray(images)),
        b64content = fs.readFileSync(imagePath, { encoding: "base64" });

      console.log("uploading an image...");
      console.log("uploading image : " + imagePath);

      T.post(
        "media/upload",
        { media_data: b64content },
        function (err, data, response) {
          if (err) {
            console.log("error:", err);
          } else {
            let randStatus = getRandomText(";", "tweets.txt");
            console.log("\n");
            console.log("Random Status :");
            console.log(randStatus + "\n");
            console.log("\n");

            // post status
            T.post(
              "statuses/update",
              {
                status: randStatus,
                media_ids: new Array(data.media_id_string),
              },
              function (err, data, response) {
                if (err) {
                  console.log("error:", err);
                } else {
                  console.log("posted an image!\n");
                  console.log("\n");
                }
              }
            );
          }
        }
      );
    }
  });
}

tweetRandomImage();