gpt4 book ai didi

node.js - NodeJs 无法捕获错误

转载 作者:太空宇宙 更新时间:2023-11-04 00:48:44 25 4
gpt4 key购买 nike

我使用模块omdb我在使用它时很难发现错误。该错误是由以下两种方法之一引起的,但堆栈跟踪未显示是哪一种。此外,没有一个 console.log 被触发,所以我假设我在尝试处理可能的错误时犯了一些错误。 omdb.poster 的描述是:

Returns a readable stream of the poster JPEG.

function downloadImage(show, callback) {
var filename = show.title + + " " + show.year + ".jpg";
var path = "./images/" + filename;
omdb.poster(show)
.pipe(fs.createWriteStream(path))
.on('error', function(err) {
console.log("Download Omdb image err: " + err + " for " + show.title + " " + show.year);
callback(err, null);
})
.on('close', function() {
callback(null, path);
});

}

function getOmdbInfo(show, callback) {
omdb.get(show, true, function(err, movie) {
if(err) {
console.log("err:" + show.title + " " + show.year);
callback(err, null);
}
if(!movie) {
console.log("No OMDB info results for: " + show.title + " " + show.year);
callback('Movie not found!', null);
} else {
callback(null, movie);
}
});
}

错误:

\node_modules\needle\lib\needle.js:81
throw new TypeError('URL must be a string, not ' + uri);
^
TypeError: URL must be a string, not null
at Object.Needle.request (\node
_modules\omdb\node_modules\needle\lib\needle.js:81:13)
at Object.exports.(anonymous function) [as get] (\node_modules\omdb\node_modules\needle\lib\needle.js:425:19)
at \node_modules\omdb\index.js:251:26
at \node_modules\omdb\index.js:198:13
at done (\node_modules\omdb\node_modules\needle\lib\needle.js:234:9)
at PassThrough.<anonymous> (\node_modules\needle\lib\needle.js:363:11)
at PassThrough.emit (events.js:129:20)
at _stream_readable.js:908:16
at process._tickCallback (node.js:355:11)

最佳答案

出现问题是因为我在尝试通过管道传输文件后捕获了订单。通过更改此:

omdb.poster(show)
.pipe(fs.createWriteStream(path))
.on('error', function(err) {
console.log("Download Omdb image err: " + err + " for " + show.title + " " + show.year);
callback(err, null);
})
.on('close', function() {
callback(null, path);
});

对此:

omdb.poster(show)
.on('error', function(err) {
console.log("Download OMDB image err: " + err + " for " + show.title + " " + show.year);
callback(err, null);
})
.pipe(fs.createWriteStream(path))
.on('close', function() {
callback(null, path);
});

错误已被捕获。

关于node.js - NodeJs 无法捕获错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33486445/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com