【问题标题】:Error here TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type object此处出错 TypeError [ERR_INVALID_ARG_TYPE]:“路径”参数必须是字符串类型。接收到的类型对象
【发布时间】:2021-01-05 20:00:50
【问题描述】:

您好,我正在尝试使用以下给定代码将 base 64 文件从 NodeJs 上传到 firebase 存储(谷歌云存储)。但我收到一个错误说明

此处出现错误 TypeError [ERR_INVALID_ARG_TYPE]:“路径”参数必须 是字符串类型。在 validateString 处接收到类型对象 (内部/validators.js:125:11)

代码是:

const key = require('../key.json');
const {Storage} = require('@google-cloud/storage');
//var gcs = require('@google-cloud/storage');
exports.uploadVideo = functions.https.onRequest((req, res) => {

    const gcs =new Storage({
        projectId: "<my-project-id>",
        keyFilename: key
      });

      const bucket = gcs.bucket("example.appspot.com");
      const gcsname = 'test.pdf';
      const file = bucket.file(gcsname);
      var pdfdata = "xNC9YUmVmU3RtIDE1NzQ+Pg0Kc3RhcnR4cmVmDQoyMTY5DQolJUVPRg==";
      var buff = Buffer.from(pdfdata, 'binary').toString('utf-8');
      const stream = file.createWriteStream({
        metadata: {
          contentType: 'application/pdf'
        }
      });

      stream.on('error', (err) => {
        console.log("Error here",err);
      });
      stream.on('finish', () => {
        console.log(gcsname);
      });
      stream.end(new Buffer.from(buff, 'base64'));})

有人可以建议我出路吗?我无法理解这个问题。 谢谢

【问题讨论】:

  • 你能提供你的 package.json 文件吗?
  • @google-cloud/storage 已安装。如果您想寻找其他内容,我可以发布它。
  • @waseemTahir 你明白了吗?我也有同样的问题...
  • @Lion789 不,很遗憾我做不到,而且我的截止日期很紧,所以我在前端实现了它,没有进一步浪费我的时间。
  • 感谢您的回复,是的,我只是忽略了使用带有 gcloud 的 winston

标签: node.js firebase google-cloud-functions google-cloud-storage firebase-admin


【解决方案1】:

如果您使用 "react-scripts" "^3.x.x" 将其替换为 "^3.4.1" 版本或最新版本,这将得到修复。 请确保您在安装 npm/yarn 之前删除了 node_modules 文件夹

【讨论】:

    【解决方案2】:

    正如错误所说“TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined"

    它试图从路径中抓取,您应该在查找路径时将这些变量添加到您的 .env 文件到根目录中。它将抓取正确的变量。如果您仍有困难,请告诉我。

    您可以参考Express Routing with Google Cloud Functions了解更多详情。

    【讨论】:

      【解决方案3】:

      这意味着程序期望 path 参数的类型为 string,例如:"path/to/whatever"。你给它一个对象 - 例如:{ path: "path/to/whatever" }

      【讨论】:

      • 这就是错误本身的含义。但根据文档,它应该以这种方式完成。我也没有给出任何路径,它是由内部谷歌库本身提取的,可能来自文件对象。
      • 您能否提供指向您所指文档的链接?
      • 你看到了吗? stackoverflow.com/questions/42879012/…。我认为这里接受的答案就是您要寻找的答案
      • github.com/googleapis/google-cloud-node/issues/2334 这是我认为对我有用的来源,但到目前为止还没有成功。这也不是官方文档。找不到我所需用例的任何官方文档。
      • 好的。将您的代码与您提供的 Github 示例逐行进行比较。最后一行似乎有所不同。你的应该改成stream.end(new Buffer(buff, 'base64'));
      猜你喜欢
      • 1970-01-01
      • 2020-09-21
      • 2020-07-13
      • 1970-01-01
      • 1970-01-01
      • 2020-08-12
      • 2020-10-21
      • 2021-09-08
      相关资源
      最近更新 更多