异步操作后 Mongodb 中的文件链接数组为空
Array of files' links is empty in Mongodb after asynchronous operations
我的 Apollo 变异函数获取 2 个文件数组作为参数。然后我将它写入文件系统,将它们的位置推入数组。之后,我希望将数组写入 MongoDB,但 mongo 由于异步而具有空字段。
我该如何处理?
const { createWriteStream } = require("fs");
const path = require('path');
const Post = require('../../models/Post');
const checkAuth = require('../../utils/check-auth');
module.exports = {
Mutation: {
async addPost(_, { postInput: { title, description, pictures, panoramas, price, location } }, ctx) {
const anon = checkAuth(ctx);
let pics = [];
let pans = [];
pictures.map(async (el) => {
const { createReadStream, filename } = await el;
await new Promise(res =>
createReadStream()
.pipe(createWriteStream(path.join("static/images", filename)))
.on("close", res)
);
pics.push(`static/images/, ${filename}`);
})
panoramas.map(async (el) => {
const { createReadStream, filename } = await el;
await new Promise(res =>
createReadStream()
.pipe(createWriteStream(path.join("static/images", filename)))
.on("close", res)
);
pans.push(path.join("static/images", filename));
})
const newPost = new Post({
title,
description,
price,
pictures: pics,
panoramas: pans,
createdAt: new Date().toISOString(),
userId: anon.id,
location,
});
const res = await newPost.save();
console.log(res)
return true;
}
}
}
您应该等到所有承诺都解决了,然后才继续创建新文档。
async function addPost(_, {postInput: {title, description, pictures, panoramas, price, location}}, ctx) {
const anon = checkAuth(ctx);
let pans = [];
let pics = [];
pictures.map(async (el) => {
pics.push(
new Promise(async (resolve, reject) => {
const {createReadStream, filename} = await el;
await new Promise(res =>
createReadStream()
.pipe(createWriteStream(path.join("static/images", filename)))
.on("close", res)
);
resolve(`static/images/, ${filename}`);
})
)
});
await Promise.all(pics);
panoramas.map(async (el) => {
pans.push(
new Promise(async (resolve, reject) => {
const {createReadStream, filename} = await el;
await new Promise(res =>
createReadStream()
.pipe(createWriteStream(path.join("static/images", filename)))
.on("close", res)
);
resolve(path.join("static/images", filename));
}));
});
await Promise.all(pans);
const newPost = new Post({
title,
description,
price,
pictures: pics,
panoramas: pans,
createdAt: new Date().toISOString(),
userId: anon.id,
location,
});
const res = await newPost.save();
console.log(res)
return true;
}
这是一个简单的示例,我建议您稍微清理一下并添加某种错误处理。
我的 Apollo 变异函数获取 2 个文件数组作为参数。然后我将它写入文件系统,将它们的位置推入数组。之后,我希望将数组写入 MongoDB,但 mongo 由于异步而具有空字段。 我该如何处理?
const { createWriteStream } = require("fs");
const path = require('path');
const Post = require('../../models/Post');
const checkAuth = require('../../utils/check-auth');
module.exports = {
Mutation: {
async addPost(_, { postInput: { title, description, pictures, panoramas, price, location } }, ctx) {
const anon = checkAuth(ctx);
let pics = [];
let pans = [];
pictures.map(async (el) => {
const { createReadStream, filename } = await el;
await new Promise(res =>
createReadStream()
.pipe(createWriteStream(path.join("static/images", filename)))
.on("close", res)
);
pics.push(`static/images/, ${filename}`);
})
panoramas.map(async (el) => {
const { createReadStream, filename } = await el;
await new Promise(res =>
createReadStream()
.pipe(createWriteStream(path.join("static/images", filename)))
.on("close", res)
);
pans.push(path.join("static/images", filename));
})
const newPost = new Post({
title,
description,
price,
pictures: pics,
panoramas: pans,
createdAt: new Date().toISOString(),
userId: anon.id,
location,
});
const res = await newPost.save();
console.log(res)
return true;
}
}
}
您应该等到所有承诺都解决了,然后才继续创建新文档。
async function addPost(_, {postInput: {title, description, pictures, panoramas, price, location}}, ctx) {
const anon = checkAuth(ctx);
let pans = [];
let pics = [];
pictures.map(async (el) => {
pics.push(
new Promise(async (resolve, reject) => {
const {createReadStream, filename} = await el;
await new Promise(res =>
createReadStream()
.pipe(createWriteStream(path.join("static/images", filename)))
.on("close", res)
);
resolve(`static/images/, ${filename}`);
})
)
});
await Promise.all(pics);
panoramas.map(async (el) => {
pans.push(
new Promise(async (resolve, reject) => {
const {createReadStream, filename} = await el;
await new Promise(res =>
createReadStream()
.pipe(createWriteStream(path.join("static/images", filename)))
.on("close", res)
);
resolve(path.join("static/images", filename));
}));
});
await Promise.all(pans);
const newPost = new Post({
title,
description,
price,
pictures: pics,
panoramas: pans,
createdAt: new Date().toISOString(),
userId: anon.id,
location,
});
const res = await newPost.save();
console.log(res)
return true;
}
这是一个简单的示例,我建议您稍微清理一下并添加某种错误处理。