Node.js ';意外的文件结尾';使用jimp将项目推送到数组中

Node.js ';意外的文件结尾';使用jimp将项目推送到数组中,node.js,Node.js,我正在尝试将多个图像叠加到一个图像上。它在某种程度上是有效的,但它并没有按照我所希望的方式发挥作用 global.images = []; fs.readdirSync('./images').forEach(function(file) { images.push(file); if (debug) { console.log(chalk.green('[Debug] Pushed ' + chalk.blue(file) + ' to the array.

我正在尝试将多个图像叠加到一个图像上。它在某种程度上是有效的,但它并没有按照我所希望的方式发挥作用

global.images = [];

fs.readdirSync('./images').forEach(function(file) {
    images.push(file);

    if (debug) {
        console.log(chalk.green('[Debug] Pushed ' + chalk.blue(file) + ' to the array.'));
    }
});
executeEdit(images);

function executeEdit(list) {
    var jimps = [];
    var x = 0;

    for (var i = 0; i < images.length; i++) {
        x = x + 150;

        setTimeout((function(i) {
            return function() {
                jimps.push(jimp.read('images/' + images[i]));
            }
        })(i), 10 * x);
    }

提示是消息
UnhandledPromiseRejectionWarning
:您缺少
jimp
实际上是异步的(回调或承诺)。在您的情况下,由于没有向
jimp.read()
传递回调,因此它假定使用了承诺,但您没有正确使用它们

如果替换
//console.log(这个.u整数)通过
console.log(jimps)
,您将看到如下内容:

[ Promise { <pending> } ]
[ Promise { <Jimp 32x32> }, Promise { <pending> } ]
.... 
因此,这将打印以下内容

[Debug] Pushed 7035a55d06033e435be112c0969b1820.png  to the array
[Debug] Pushed aa5ef861d490a11fe20806e83c6dc64b.png  to the array
[Debug] Pushed crayon.png  to the array
waiting for executeEdit() to terminate
<Jimp 32x32>
<Jimp 48x48>
<Jimp 256x256>
[Debug]将7035a55d06033e435be112c0969b1820.png推送到阵列中
[调试]将AA5EF861D490A11FE2086E83C6DC64B.png推送到阵列
[Debug]将crayon.png推送到数组中
正在等待executedit()终止
所以这3个文件(在我的例子中)是同步读取的,然后调用executedit()。我们构建一个promise对象数组,然后调用promise.all(),它将等待所有承诺都得到解决。值
loadedimgs
是一个数组,包含每个承诺的结果(与承诺的顺序相同)。然后,您可以根据需要在该数组上循环(这里我使用了
map
调用,但for循环就可以了


我希望这能有所帮助。

谢谢您的回复!但是,我在运行此代码时遇到了问题。我仍然会遇到相同的错误
意外的文件结尾
,以及错误
wait jimp.read('images/'+images[I])^^^^^ SyntaxError:await仅在异步函数
中有效。以下是我当前的代码:-如果您知道如何解决此问题,它将非常有帮助:^)文件意外结束是否是由于某个映像不正确或损坏所致?您还可以告诉我您使用的是哪个版本的node吗?我粘贴的代码位于名为
test.js
的文件中,我使用
node test.js
运行该文件时没有问题。我的node版本是v10.8.0。我将查看粘贴库。这可能与节点im的版本有关:im on
v8.11.2
://这很奇怪。我刚刚安装了v8.11.2,并从pastebin运行了该文件。我没有收到任何king的错误消息,也没有发布到async或意外的eof:(输出在这里(未定义的是法线,因为在处理承诺时图像[I]不再有效)您可以尝试使用一些不同的图像(包括背景)吗我会很快制作一个youtube视频来展示我的问题给我一分钟
var fs = require('fs');
var jimp = require('jimp');

const images = [];
fs.readdirSync('./images').forEach(function(file) {
    images.push(file);
    console.log('[Debug] Pushed', file, ' to the array');
});

executeEdit(images);
console.log('waiting for executeEdit() to terminate');

function executeEdit(list) {
  let jimp_read_promises = []

  for (var i = 0; i < images.length; i++) {
      jimp_read_promises.push( jimp.read('images/' + images[i]))
  }

  Promise.all(jimp_read_promises).then( loadedimgs => {
    loadedimgs.map( img => {
      console.log( img )
    })
  })
}
[Debug] Pushed 7035a55d06033e435be112c0969b1820.png  to the array
[Debug] Pushed aa5ef861d490a11fe20806e83c6dc64b.png  to the array
[Debug] Pushed crayon.png  to the array
waiting for executeEdit() to terminate
<Jimp 32x32>
<Jimp 48x48>
<Jimp 256x256>