【问题标题】:Argument of type '{ action: string; expires: string; }' is not assignable to parameter of type 'GetSignedUrlConfig''{ action: string; 类型的参数过期:字符串; }' 不可分配给“GetSignedUrlConfig”类型的参数
【发布时间】:2020-04-28 22:11:21
【问题描述】:

我正在尝试检索上传的新文件的下载 url,以便将其写入我的数据库。 我关注了this answerofficial docs,但我的函数出现错误。

这些是我的导入:

import * as functions from "firebase-functions";
import * as admin from "firebase-admin";
import { FieldValue } from "@google-cloud/firestore";
import { _namespaceWithOptions } from "firebase-functions/lib/providers/firestore";
admin.initializeApp(functions.config().firebase);
const db = admin.firestore();
const defaultStorage = admin.storage();

这是我目前想要的云功能:

exports.writeFileToDatabase = functions.storage
  .object()
  .onFinalize(object => {
    const bucket = defaultStorage.bucket();
    const file = bucket.file(object.name as string);

    const options = {
        action: 'read',
        expires: '03-17-2025'
      };

    return file.getSignedUrl(options)
    .then(results => {
      const url = results[0];

      console.log(`The signed url is ${url}.`);
      return true;
    })
  });

但是当将options 传递给getSignedUrl 时,我得到了这个错误:

Argument of type '{ action: string; expires: string; }' is not assignable to parameter of type 'GetSignedUrlConfig'

我在results 上也收到错误消息:

Parameter 'results' implicitly has an 'any' type

与我用作参考的示例不同,我看不出我在做什么。

【问题讨论】:

  • 您能否编辑问题以准确显示您如何在此处导入或使用模块?尤其是 Cloud Storage 或 Firebase Admin SDK。您的项目的 TypeScript 配置也可能存在问题。您不必具体说明事物的类型 - TypeScript 应该都能理解它们。
  • @DougStevenson 我已经编辑了我的问题并添加了我的导入。但是 - 我实际上最终解决了这个问题,我不确定它为什么会有所作为(我的解决方案)。如果您了解原因,将不胜感激。

标签: typescript firebase google-cloud-functions firebase-storage


【解决方案1】:

这是因为您使用的是 TypeScript,而您所指的示例是使用 JavaScript。您应该按如下方式导入类型GetSignedUrlConfig

import { GetSignedUrlConfig } from '@google-cloud/storage';

然后做

const options: GetSignedUrlConfig = {
  action: 'read',
  expires: '03-17-2025'
};

【讨论】:

  • 你确定吗? TypeScript 完全能够根据对象的“形状”推断对象的类型。选项对象的形状对我来说看起来很好,并且与文档中的内容相匹配。 developer.android.com/guide/components/…
  • @DougStevenson 我已经对其进行了测试,但没有明确指定 options 对象的类型,我得到的错误与 OP 相同。也许我的 Cloud Functions 的 TypeScript 设置不完整,但我遵循了文档,所以我认为它没问题。 PS:您评论中的链接是正确的吗?
  • 我的项目在推断类型时从来没有遇到过问题。如果您传递裸对象,它只会起作用。 TypeScript 了解配置的预期并检查键和值以确保它们符合云 SDK 提供的 TypeScript 绑定。您的项目 TS 配置可能有问题。
  • @RenaudTarnec 谢谢你的回答。在看到它之前,我能够用一个非常简单的解决方案来解决它。不过老实说,我不确定为什么我所做的更改实际上会产生任何影响。如果您这样做,希望得到澄清。
  • 我解释了为什么会在这种特殊情况下发生。
【解决方案2】:

所以,解决我的问题的方法是直接将配置对象放入方法中,如下所示:

exports.writeFileToDatabase = functions.storage
  .object()
  .onFinalize(object => {
    const bucket = defaultStorage.bucket();
    const file = bucket.file(object.name as string);

    return file.getSignedUrl({
        action: 'read',
        expires: '03-17-2025'
      })
    .then(results => {
      const url = results[0];

      console.log(`The signed url is ${url}.`);
      return true;
    })
  });

我不知道为什么它会有所作为,并且希望任何这样做的人都能得到澄清,但它只适用于这个小改动。

【讨论】:

  • 问题实际上是因为action属性被键入为'read' | 'write' | 'delete' | 'resumable'的交集,以检查您是否传递了正确的操作值。如果您像这样内联传递对象,这将起作用,因为它能够根据云 SDK 的期望推断类型。但是当你像最初那样创建一个普通对象并将它分配给一个变量时,这个类型信息就会丢失,TypeScript 只知道你给了它一个字符串值(不是特定值之一)。所以,你可以像现在这样,或者在赋值的时候声明类型
猜你喜欢
  • 1970-01-01
  • 2021-07-02
  • 2021-06-16
  • 2021-09-07
  • 1970-01-01
  • 2021-09-06
  • 1970-01-01
  • 2018-04-05
  • 2021-11-23
相关资源
最近更新 更多