Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

UnhandledPromiseRejectionWarning when calling SpeechClient.recognize #684

Closed
en4letto opened this issue Dec 11, 2020 · 3 comments
Closed
Assignees
Labels
api: speech Issues related to the googleapis/nodejs-speech API. status: investigating The issue is under investigation, which is determined to be non-trivial. type: question Request for information or clarification. Not an issue.

Comments

@en4letto
Copy link

en4letto commented Dec 11, 2020

I am using the @google-cloud/speech library and bundling an express app with webpack.

I had the proto error (as described here) and fixed it by passing {fallback: true} to the constructor.

Right now I get the error UnhandledPromiseRejectionWarning: TypeError: fetch is not a function, which is triggered when in my code I execute the recognize function of the client.
Here the related portions of my code:

// THIS IS THE CONFIG PASSED TO THE CONSTRUCTOR
authSTTConfig: {
        projectId: process.env.STT_PROJECT_ID,
        keyFilename: process.env.STT_KEY_FILENAME,
        fallback: true,
    }
//

...

class GCloudSpeechAPIv2 {
    constructor() {
        this.audioChunks = [];
        this.finalAudio = null;

        this.instanceSTT = new speech.SpeechClient(authSTTConfig);

        this.requestSTT = {
            // interimResults: true,
            config: cloudSTTConfig,
        };
    }

    async transcribeAudio() {
        this.requestSTT.audio = {
            content: this.finalAudio.toString("base64"),
        };

        // console.log("transcribeAudio");
        const [response] = await this.instanceSTT.recognize(this.requestSTT); // ERROR IS THROWN HERE
        const transcription = response.results
            .map((result) => result.alternatives[0].transcript)
            .join("");
        return transcription;
    }

...

Environment details

  • OS: ubuntu 20.04
  • Node.js version: v12.18.4
  • npm version: 6.14.6
  • @google-cloud/speech version: 4.1.5

Steps to reproduce

  1. Implement the code to call the recognize function of the speech client
  2. Bundle the application with webpack, add the {fallback: true} option

Thanks!

@product-auto-label product-auto-label bot added the api: speech Issues related to the googleapis/nodejs-speech API. label Dec 11, 2020
@munkhuushmgl munkhuushmgl added the type: question Request for information or clarification. Not an issue. label Dec 11, 2020
@en4letto
Copy link
Author

A quick update: the problem seems to be related to node-fetch and webpack, here a more detailed analysis.

@b-loved-dreamer b-loved-dreamer added the status: investigating The issue is under investigation, which is determined to be non-trivial. label Dec 17, 2020
@b-loved-dreamer
Copy link
Contributor

Hi @en4letto, upon trying your code, a UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'toString' of null error was thrown. The 'toString' of null seems to be the causing root of the issue and this is because your finalAudio variable is never set to anything. After I assigned this variable, the UnhandledPromiseRejectionWarning went away. If you want to set your credentials explicitly, you can follow this sample: const client = new speech.SpeechClient({KeyFileName: 'path/to/file.json.'});. The request object is composed of config and audio objects. Please take a look at this sample.

@b-loved-dreamer
Copy link
Contributor

Closing this issue. Please reopen if needed.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api: speech Issues related to the googleapis/nodejs-speech API. status: investigating The issue is under investigation, which is determined to be non-trivial. type: question Request for information or clarification. Not an issue.
Projects
None yet
Development

No branches or pull requests

3 participants