【问题标题】:Uploading documents, pdf's to google cloud using firebase. Get error使用 firebase 将文档、pdf 上传到谷歌云。获取错误
【发布时间】:2019-12-16 10:46:34
【问题描述】:

我正在尝试使用 busboy / express.js 将简单文档上传到谷歌云。

我收到此错误。

Error: Cannot find module 'busboy/lib/types/node_modules/dicer'

这是请求的代码。

// Upload a document for claim
exports.uploadDocument = (req, res) => {
  const BusBoy = require("busboy");

  const path = require("path");

  const os = require("os");

  const fs = require("fs");

  const busboy = new BusBoy({ headers: req.headers });

  let DocumentToBeUploaded = {};
  let DocumentFileName;
  // change this section to storing pdfs and docs etc

  busboy.on("file", (fieldname, file, filename) => {
    console.log(fieldname, file, filename);

    const documentExtension = filename.split(".")[
      filename.split(".").length - 1
    ];
    // 32756238461724837.png
    DocumentFileName = `${Math.round(
      Math.random() * 1000000000000
    ).toString()}.${documentExtension}`;
    const filepath = path.join(os.tmpdir(), DocumentFileName);
    DocumentToBeUploaded = { filepath, mimetype };
    file.pipe(fs.createWriteStream(filepath));
  });
  busboy.on("finish", () => {
    admin
      .storage()
      .bucket()
      .upload(DocumentToBeUploaded.filepath, {
        resumable: false,
        metadata: {
          metadata: {
            contentType: DocumentToBeUploaded
          }
        }
      })
      .then(() => {
        const docUrl = `https://firebasestorage.googleapis.com/v0/b/${config.storageBucket}/o/${DocumentFileName}?alt=media`;
        return db.doc(`/users/${req.Claim.ClaimId}`).update({ docUrl });
      })
      .then(() => {
        return res.json({ message: "document uploaded successfully" });
      })
      .catch(err => {
        console.error(err);
        return res.status(500).json({ error: "something went wrong" });
      });
  });
  busboy.end(req.rawBody);
};

目前只是尝试上传一个非常简单的文本文档。当然这不会那么困难,我在某个地方犯了一个简单的错误。

感谢帮助:)

【问题讨论】:

    标签: javascript firebase api express busboy


    【解决方案1】:

    你需要安装busyboy:

    npm i busboy
    

    您可以在以下链接中找到有关此 npm 包的更多信息:

    https://www.npmjs.com/package/busboy

    【讨论】:

    • 奇怪的是,这很有效(我在部署时有服务员,但结果是没有服务)。我现在得到了未定义的 mimetype
    猜你喜欢
    • 1970-01-01
    • 2019-12-27
    • 1970-01-01
    • 1970-01-01
    • 2018-08-01
    • 1970-01-01
    • 2022-08-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多