首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >异步操作后Mongodb文件链接数组为空

异步操作后Mongodb文件链接数组为空
EN

Stack Overflow用户
提问于 2020-01-21 02:59:41
回答 1查看 34关注 0票数 0

我的Apollo变异函数获得2个文件数组作为参数。然后我将其写入文件系统,将它们的位置推入数组。在那之后,我想用MongoDB编写数组,但是由于异步,mongo有空字段。我该怎么处理呢?

代码语言:javascript
复制
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;
    }
  }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-21 03:21:58

你应该等到所有的承诺都解决了,然后才开始创建一个新的文档。

代码语言:javascript
复制
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;
}

这是一个很快的例子,我建议你把它清理一下,并添加一些错误处理。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59829412

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档