Почему я получаю сообщение «Функция вернула неопределенное значение, ожидаемое обещание или значение»? Я не могу понять. Может быть, я что-то упускаю.
function getImage(url) {
console.info('Begin get image');
return new Promise(function(resolve, reject) {
https.get(url, res => {
// Initialise an array
const bufs = [];
// Add the data to the buffer collection
res.on('data', function (chunk) {
console.info('on data image');
bufs.push(chunk)
});
// This signifies the end of a request
res.on('end', function () {
console.info('on end image');
// We can join all of the 'chunks' of the image together
const data = Buffer.concat(bufs);
resolve(data);
});
})
// Inform the callback of the error.
.on('error', function (error) {
reject(error);
});
});
}
Здесь я вызываю функцию:
getImage(imageURL)
.then( dataImg => {
console.info('image get get');
(...)
}
Я должен получить dataImg и выполнить операции.
Я протестировал следующий код с помощью узла и получил буфер изображения. Может быть, проверьте, не пропустили ли вы где-то скобки.
var https = require("https");
function getImage(url) {
console.info('Begin get image');
// Return the loading-promise
return (new Promise((resolve, reject) => {
https.get(url, (res) => {
// Initialise an array to store the image-data
const bufs = [];
// Add the data to the buffer collection
res.on('data', function(chunk) {
console.info('on data image');
// Push the recieved chunks into the array
bufs.push(chunk)
});
// This signifies the end of a request
res.on('end', function() {
console.info('on end image');
// We can join all of the 'chunks' of the image together
const data = Buffer.concat(bufs);
// Return the image-buffer
resolve(data);
});
})
// Inform the callback of the error.
.on('error', function(error) {
reject(error);
});
}));
}
(function() {
getImage("https://preview.redd.it/utrlx02sx2431.jpg?width=640&crop=smart&auto=webp&s=8ed20013607e0d6019f59ab01b0e4930eb913351")
.then((dataImg) => {
console.info('image get get');
console.info(dataImg);
});
})();
Консольный вывод:
ab@xy:~/test$ node testrun.js
Begin get image
on data image
on data image
on end image
image get get
<Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 01 01 00 00 00 00 00 00 ff e1 00 42 45 78 69 66 00 00 4d 4d 00 2a 00 00 00 08 00 01 87 69 00 04 00 00 00 01 00 00 ... 20311 more bytes>
Спасибо. Да на самом деле именно в моей тогда оговорке у меня ошибка. Я попытался ввести полученные данные в виде изображения base 64, которое не...