async/await 问题,当我的 URL 插入数据库时,我得到的是空对象
async/await problem, when the my URLs are inserting on database I am getting empty object
我在我的路线中创建了一个函数,它将所有图像上传到 Cloudinary 第 3 方库,它将 return 所有 URL 链接,我将所有链接推送到我的 URL 变量中,然后链接将存储到数据库中。
我希望在我的 URL 变量上的链接可用之前,它不会插入到我的数据库中。 我对如何使用 async/await 或使用承诺
这是我的功能路线。我正在使用 node、express、multer。
app.post('/addProduct', async (req, res, next) => {
let urls = [];
async function sendImagesToCloudinary() {
for (let file of req.files) {
await cloudinary.uploader.upload(
file.path,
{
public_id: `${Date.now()}`,
resource_type: 'auto'
}
).then(result => {
//del files after upload on cloudinary
fs.unlink(file.path, function (err) {
if (err) {
console.log(err);
}
});
urls.push(result.url);
})
.catch(err => {
console.log(err);
});
}
res.json(urls);
}
sendImagesToCloudinary();
// Publish on database
const result = await unityMartMediaCollection.insertOne({ urls: urls })
res.json(result)
});
值得 (a) 将各种异步操作梳理成更小、更清晰、可测试的函数,以及 (b) 仅使用一种 promise 语法风格...
// isolated version of he OP upload
async function upload(file) {
const params = { public_id: `${Date.now()}`, resource_type: 'auto' }
return cloudinary.uploader.upload(file.path, params);
}
// this promisify's fs.unlink (probably natively available in fs.promise)
async unlink(file) {
return new Promise((resolve, reject) => {
fs.unlink(file.path, error => error? reject(error) : resolve());
});
}
// upload, then unlink if upload succeeds, return the url upload result
// catch errors here, so other concurrent uploads can continue
async uploadAndUnlink(file) {
try {
const url = await upload(file);
await unlink(file);
return url
} catch (err) {
console.log(err);
}
}
// implement the route, process the files concurrently
app.post('/addProduct', async (req, res, next) => {
const promises = req.files.map(file => uploadAndUnlink(file));
const urls = await Promise.all(promises);
const result = await unityMartMediaCollection.insertOne({ urls: urls })
res.json(result);
});
我冒昧地删除了 URL-producing 方法中对 res 的 .json 调用。其余代码的强烈暗示是目标是 return(对客户端)unityMartMediaCollection
调用的结果。
我在我的路线中创建了一个函数,它将所有图像上传到 Cloudinary 第 3 方库,它将 return 所有 URL 链接,我将所有链接推送到我的 URL 变量中,然后链接将存储到数据库中。
我希望在我的 URL 变量上的链接可用之前,它不会插入到我的数据库中。 我对如何使用 async/await 或使用承诺
这是我的功能路线。我正在使用 node、express、multer。
app.post('/addProduct', async (req, res, next) => {
let urls = [];
async function sendImagesToCloudinary() {
for (let file of req.files) {
await cloudinary.uploader.upload(
file.path,
{
public_id: `${Date.now()}`,
resource_type: 'auto'
}
).then(result => {
//del files after upload on cloudinary
fs.unlink(file.path, function (err) {
if (err) {
console.log(err);
}
});
urls.push(result.url);
})
.catch(err => {
console.log(err);
});
}
res.json(urls);
}
sendImagesToCloudinary();
// Publish on database
const result = await unityMartMediaCollection.insertOne({ urls: urls })
res.json(result)
});
值得 (a) 将各种异步操作梳理成更小、更清晰、可测试的函数,以及 (b) 仅使用一种 promise 语法风格...
// isolated version of he OP upload
async function upload(file) {
const params = { public_id: `${Date.now()}`, resource_type: 'auto' }
return cloudinary.uploader.upload(file.path, params);
}
// this promisify's fs.unlink (probably natively available in fs.promise)
async unlink(file) {
return new Promise((resolve, reject) => {
fs.unlink(file.path, error => error? reject(error) : resolve());
});
}
// upload, then unlink if upload succeeds, return the url upload result
// catch errors here, so other concurrent uploads can continue
async uploadAndUnlink(file) {
try {
const url = await upload(file);
await unlink(file);
return url
} catch (err) {
console.log(err);
}
}
// implement the route, process the files concurrently
app.post('/addProduct', async (req, res, next) => {
const promises = req.files.map(file => uploadAndUnlink(file));
const urls = await Promise.all(promises);
const result = await unityMartMediaCollection.insertOne({ urls: urls })
res.json(result);
});
我冒昧地删除了 URL-producing 方法中对 res 的 .json 调用。其余代码的强烈暗示是目标是 return(对客户端)unityMartMediaCollection
调用的结果。