Viv*_*mar 6 mime mongoose node.js
router.post('/', (req, res)=>{
let searchTerm = req.body.search;
const kwords = new keywords({
keyword: searchTerm
});
keywords.findOne({keyword: searchTerm}, function(err, key){
if(err) throw err;
else if(!key){
kwords.save();
}
});
google.list({
keyword: searchTerm,
num: 15,
detail: false,
nightmare: {
show: false
}
})
.then(function(images){
images.forEach(function(image, index){
let url = image.url;
//compression
Jimp.read(url, function(err, img){
if(err) throw err;
if(img){
img.resize(256, 256)
.quality(60)
.greyscale()
.write("public/images/"+searchTerm+index+".jpg");
}
});
});
res.redirect('/list');
}).catch(function(err){
console.log('err', err);
});
Run Code Online (Sandbox Code Playgroud)
});
我正在使用图像抓取工具和 jimp 库。在 Jimp.read() 中,当 url 没有 mime type 时,img 变为 null,因此大多数情况下我不是获取 15 个图像,而是获取 13,14 个图像。有人请帮我解决这个问题,以便我可以获得所有 15 张图片。
小智 0
你应该使用 Promise 方法而不是回调
google.list({
keyword: searchTerm,
num: 15,
detail: false,
nightmare: {
show: false
}
})
.then(function(images){
images.forEach(function(image, index){
let url = image.url;
//compression
Jimp.read(url)
.then(function(img) {
if(img){
img.resize(256, 256)
.quality(60)
.greyscale()
.write("public/images/"+searchTerm+index+".jpg");
}
}).catch(console.error)
});
res.redirect('/list');
})
.catch(function(err){
console.log('err', err);
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1381 次 |
| 最近记录: |