Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReferenceError: self is not defined when trying to call a grpc-web function from a CLI/server application #1130

Open
bt-nia opened this issue Nov 9, 2022 · 3 comments

Comments

@bt-nia
Copy link

bt-nia commented Nov 9, 2022

Hi!

I'm encountering an issue that is probably related to: #728
I'm not sure if this related to grpc-web, but wanted to make sure that the use case is supported. I'm trying to create a server that interacts with another server via grpc-web.

Here's my code:

import express, { Express, Request, Response } from 'express';
import dotenv from 'dotenv';
import * as gauth from 'google-auth-library';
​
import { MyServiceClient } from '../../src/ui/client/src/proto/my_pb_service';
import * as pb from '../../src/ui/client/src/proto/my_pb';
import { Observable } from 'rxjs';
​
dotenv.config();
​
const app: Express = express();
const port = process.env.PORT;
​
app.get('/', (req: Request, res: Response) => {
  var empty: string[];
  empty = [];
  res.send('Express + TypeScript Server');
​
  console.log("creating client")
  let r = new pb.CollectAndScanRequest();
  r.setResourceGroupNamesList(empty);
  var client = new MyServiceClient('https://my-dev.backend.url/api')
​
  console.log("client created")
  client.collectAndScan(r, (err, res) => {
    if (err !== null) {
      console.error(`collectAndScanAll: ${err}`);
    }
    if (res === null) {
      console.error(`collectAndScanAll: unexpected null response`);
    }
    console.log(`collectAndScanAll: ${res}`);
  });
  console.log("Done")
});
​
app.listen(port, () => {
  console.log(`⚡️[server]: Server is running at https://localhost:${port}`);
});

Here's the error I see:

creating client
client created
ReferenceError: self is not defined
    at new e (REDACTED/src/ui/client/node_modules/@improbable-eng/grpc-web/dist/grpc-web-client.js:1:16017)
    at REDACTED/src/ui/client/node_modules/@improbable-eng/grpc-web/dist/grpc-web-client.js:1:15931
    at REDACTED/src/ui/client/node_modules/@improbable-eng/grpc-web/dist/grpc-web-client.js:1:15942
    at o (REDACTED/src/ui/client/node_modules/@improbable-eng/grpc-web/dist/grpc-web-client.js:1:15312)
    at t.makeDefaultTransport (REDACTED/src/ui/client/node_modules/@improbable-eng/grpc-web/dist/grpc-web-client.js:1:15405)
    at e.createTransport (REDACTED/src/ui/client/node_modules/@improbable-eng/grpc-web/dist/grpc-web-client.js:1:8356)
    at new e (REDACTED/src/ui/client/node_modules/@improbable-eng/grpc-web/dist/grpc-web-client.js:1:7911)
    at t.client (REDACTED/src/ui/client/node_modules/@improbable-eng/grpc-web/dist/grpc-web-client.js:1:7619)
    at t.unary (REDACTED/src/ui/client/node_modules/@improbable-eng/grpc-web/dist/grpc-web-client.js:1:24075)
    at MyServiceClient.collectAndScan (REDACTED/src/ui/client/src/proto/my_pb_service.js:60:21)

Now, I'm new to TS/JS so this error and trace log is not really helpful for me. It could also be related to instantiating the grpc client, but then, I copied this from a working webfrontend code.

Does grpc-web support non browser invocation? Could this be related to webpack config?
Given there is a PR for something similar, how/where would I need to define self?

@johanbrandhorst
Copy link
Contributor

I think you may need to specify that you want to use a specific transport, namely the node transport. See https://github.com/improbable-eng/grpc-web/tree/master/client/grpc-web-node-http-transport

@rehanahamed240
Copy link

i am also facing same issue when i am using gprc,

below is the error message

self is not defined
ReferenceError: self is not defined
at new e (E:\StabilityAi\node_modules@improbable-eng\grpc-web\dist\grpc-web-client.js:1:16017)
at E:\StabilityAi\node_modules@improbable-eng\grpc-web\dist\grpc-web-client.js:1:15931
at E:\StabilityAi\node_modules@improbable-eng\grpc-web\dist\grpc-web-client.js:1:15942
at o (E:\StabilityAi\node_modules@improbable-eng\grpc-web\dist\grpc-web-client.js:1:15312)
at t.makeDefaultTransport (E:\StabilityAi\node_modules@improbable-eng\grpc-web\dist\grpc-web-client.js:1:15405)
at e.createTransport (E:\StabilityAi\node_modules@improbable-eng\grpc-web\dist\grpc-web-client.js:1:8356)
at new e (E:\StabilityAi\node_modules@improbable-eng\grpc-web\dist\grpc-web-client.js:1:7911)
at t.client (E:\StabilityAi\node_modules@improbable-eng\grpc-web\dist\grpc-web-client.js:1:7619)
at t.invoke (E:\StabilityAi\node_modules@improbable-eng\grpc-web\dist\grpc-web-client.js:1:14630)
at GenerationServiceClient.generate (E:\StabilityAi\api-interfaces-main\gooseai\generation\generation_pb_service.js:44:21)

Any help will be thankfull.

@qbacuber
Copy link

I have the same problem, it seems to me that they should use instead of self they should use globalThis.

but I guess we have to wait for a fix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants