The specified argument '{ action: string; expires: string; }' cannot be matched with the parameter type 'GetSignedUrlConfig'

My goal is to fetch the download URL for newly uploaded files in order to store it in my database. I've tried following guidance from both this answer and the official documentation, but I keep encountering errors within my function.

Below are the packages I have imported:

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();

This is how my cloud function currently looks:

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;
    })
  });

However, when passing options into getSignedUrl, I receive this error:

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

I also encounter an error regarding results, stating:

Parameter 'results' implicitly has an 'any' type

Despite referencing examples, I am unable to spot the differences causing these issues.

Answer №1

The reason for this discrepancy is due to the usage of TypeScript in your code while the referenced example employs JavaScript. In order to rectify this, you need to import the type GetSignedUrlConfig in the following manner:

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

Additionally, make sure to execute the following:

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

Answer №2

After some experimentation, I found the solution to my problem by directly inserting the config object into the method:

exports.saveFileToCloudStorage = 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;
    })
  });

Although I am unsure of the reason behind this change, it resolved the issue for me. Any explanations would be greatly appreciated!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the reason for the regeneration of the 2D array?

I have a method called generateWeights() that retrieves random values in an array; And a method named learn() that calls the changeWeights() method to alter the values in the array. Expected: Prior to invoking the learn() method, I anticipate receiving an ...

In Typescript Angular, how can I invoke a function on each element of an array as part of a sequence of Observables, and then return the total number of successful operations?

I am faced with the task of creating a parent record followed by multiple child records (order does not matter), and ending with a logging action. I am knowledgeable on how to chain single actions on an observable by mapping them together. For example: - ...

What is the method for setting autofocus to the first input element within a loop?

I am currently working on a loop to display inputs, and I would like to be able to add focus to the first input element when it is clicked. Does anyone have any suggestions on how I can select that first element and set autofocus on it? ...

Steps for executing a single test across multiple URLs using Playwright

My goal is to run a test for over 1000 URLs as quickly as possible. However, I am encountering a timeout error when the number of URLs exceeds 10. It seems like the tests are running sequentially, causing delays. Is there a way to run these tests in parall ...

Accessing data in vuex can result in Firebase SnapShot.val() returning null

I am developing an application that allows access for students, staff, and non-teaching staff. Here is how my form data is structured: formData: { name: "", email: "", password: "", select: null }, options: ["Student", "St ...

What is the correct method for retrieving a specific child element in React?

In my React project, I have created a component that consists of various subcomponents: import React, { FunctionComponent } from 'react'; import { FormControl, FormGroup, FormGroupProps, FormLabel, FormText, FormTextProps } from 'react-boots ...

In what way can I utilize const assertions to extract literal types from deeply nested objects?

Let's imagine I have a unique set of individuals: const group = { Alex: ['Ben', 'Chris'], Sarah: ['Dylan'], } as const; With the help of constant clarifications, I can define the specific types like so: type Parent = ...

Confirming that the NGRX Dictionary value is set

After upgrading from Angular 7.1.4 to 8.2.0 and Typescript from 3.1.6 to 3.5.3, I encountered an issue with identification of array items. Prior to the upgrade, TypeScript correctly recognized that the array item was not undefined. However, post-upgrade, ...

Prefer utilizing Null over Undefined

In my Angular 7 application, I have defined the following interface: export interface Request { limit?: number; offset?: number; } Additionally, there is a service method implemented like so: public get(request: Request): Observable<Response>& ...

The selectedIndex attribute is failing to function properly for the mat-tab-nav-bar tabs in Angular Material

I've implemented the tab navigation code as shown below: <nav mat-tab-nav-bar [selectedIndex]="0"> <a mat-tab-link *ngFor="let link of navLinks; let i = index;" [routerLink]="link.path" routerLinkActive #rla="rou ...

Refining Typescript type with specific error for generics

Seeking assistance to comprehend the situation: TS playground The situation involves a store with an exec method, where narrowing down the type of exec param is crucial for a sub process. However, an error seems to arise due to the store type being generi ...

"Omitting the parameter value when passing it to a directive in Angular

I recently developed a Directive in Angular7, but I encountered an issue when trying to pass a string value from the HTML to the directive. When using the following code snippet in my HTML: <ng-template [appValidatePermission]='CreateRole'&g ...

Ways to solve the issue: Error: Uncaught (in promise): TypeError: this.afAuth.authState.take is not a recognized function

Every time I attempt to access this page on my Ionic App, an error keeps popping up: Error: Uncaught (in promise): TypeError: this.afAuth.authState.take is not a function This issue is really frustrating as it was working perfectly fine before! I' ...

Are there any means to automatically generate placeholder methods and properties for constructor dependencies in Angular?

constructor(private a:dependencyA,private b:dependencyB,private c:dependencyC){ } Here is an example of how dependencyA is structured: export class dependencyA { showPopup: boolean; defaultProperties = { showPopup: this.showPopup, }; priva ...

Contrast between employing typeof for a type parameter in a generic function and not using it

Can you explain the difference between using InstanceType<typeof UserManager> and InstanceType<UserManager> I'm trying to understand TypeScript better. I noticed in TS' typeof documentation that SomeGeneric<typeof UserManager> ...

Using Typescript with Firebase to store and retrieve a photo's

I recently incorporated Firebase into my React project, aiming to showcase the user's profile picture upon signup or signin. However, I encountered an error while attempting to implement this feature. Below is the code snippet: https://i.sstatic.net ...

After upgrading Expo, the React Native Auth Session ceased to function

During my use of Expo SDK 48, my app successfully implemented Google and Facebook authentication with a web browser-based authentication method. Functional code: type AuthResponse = AuthSession.AuthSessionResult & { params: { access_token ...

Creating a data type restricted to utilizing property names exclusively from a specified string union:

I have a specific Enum: enum MyEnum { optionOne = 0, optionTwo = 1, optionThree = 2, optionFour = 3, } and a related Type: export type EnumNamesList = keyof typeof MyEnum; I am looking to create a type similar to this: export type EnumDataTypes = ...

The error "Cannot set property '0' of undefined" typically occurs when trying to assign a value to an

Within the code snippet below, my goal is to iterate through the contents of an array: features[0]["values_"]["geometry"]["flatCoordinates"] When running the application, all console statements outside the for-loop successfully display data. However, upon ...

Having trouble obtaining the ref.current.offsetWidth?

I have been working on creating a contextMenu. My goal is to retrieve the offsetWidth and offsetHeight from ref.current, but when I console.log it, it shows as undefined. const ContextMenu: React.FC<ContextMenuProps> = props => { const thisCom ...