gpt4 book ai didi

node.js - 异步/等待 - 数据未定义

转载 作者:行者123 更新时间:2023-12-02 18:56:33 26 4
gpt4 key购买 nike

尝试将 CSV 转换为 JSON,然后下载图像,然后将其上传到 S3 存储桶,然后更新当前的 JSON,最后生成新的 CSV。但是,代码不会等待异步函数完成,因此 const data = wait handle_image_URLs(csv); 返回为 未定义

//parse CSV - extract images, then upload to S3 bucket
const handle_image_URLs = file => {
new Promise((resolve, reject) => {
fs.createReadStream(file)
.pipe(csv_to_json())
.on("data", async data => {
// console.log("data", data);
try {
if (is_image_url(data.image_link)) {
console.log("data.image_link", data.image_link);
image_buffer = await download_image(data.image_link);
name = uuid() + ".jpg";
const location = await upload_to_S3(image_buffer, name);
//Inject back updated image url
data.image_link = location.Location;
//New generated data goes into an array
results.push(data);
}
// console.log("RESULT", results);
resolve(results);
} catch (error) {
reject(error);
}
});
});
};

express 处理 csv 上传

app.post("/", upload.single("file"), async (req, res) => {
const file = req.file;
const csv = `./uploads/${file.originalname}`;
const data = await handle_image_URLs(csv);
console.log("DATA", data);
// await json_to_csv_converter.json2csv(data, json2csvCallback);
res.status(200).json({ message: "working " });
});

最佳答案

return new Promise((resolve,reject))更新为return new Promise(async(resolve,reject))并且它应该可以工作

关于node.js - 异步/等待 - 数据未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60226868/

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