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

Using await in an async handler returns undefined to clients #22

Open
Ganitzsh opened this issue Feb 12, 2022 · 0 comments
Open

Using await in an async handler returns undefined to clients #22

Ganitzsh opened this issue Feb 12, 2022 · 0 comments

Comments

@Ganitzsh
Copy link

Ganitzsh commented Feb 12, 2022

Hello,

I have an issue with empty response to clients when using await within async functions.

Here is my proto:

syntax = "proto3";

package user;

service User {
  rpc GetUserFullName(GetUserFullNameRequest) returns (GetUserFullNameResponse) {}
}

message GetUserFullNameRequest {
  string user_id = 1;
}

message GetUserFullNameResponse {
  string full_name = 1;
}

I have this handler:

import { UserModel } from 'mongoose/models';

async function getUserFullName(ctx) {
  const user = await UserModel.findById(ctx.req.userId);

  console.log(user); // The user is properly retrieved

   ctx.res = { fullName: `${user.firstName} ${user.lastName}` };
}

Very simple so far, but upon calling it with a client generated with grpc-caller I get this error:

const response = await userServiceClient.getUserFullName({
  userId: args.userId,
});
Error: 13 INTERNAL: Cannot read property 'fullName' of undefined
    at Object.callErrorFromStatus (/node_modules/@grpc/grpc-js/src/call.ts:81:24)
    at Object.onReceiveStatus (/node_modules/@grpc/grpc-js/src/client.ts:343:36)
    at Object.onReceiveStatus (/node_modules/@grpc/grpc-js/src/client-interceptors.ts:462:34)
    at Object.onReceiveStatus (//node_modules/@grpc/grpc-js/src/client-interceptors.ts:424:48)
    at /node_modules/@grpc/grpc-js/src/call-stream.ts:323:24
    at processTicksAndRejections (node:internal/process/task_queues:78:11) {
  code: 13,
  details: "Cannot read property 'fullName' of undefined",
  metadata: Metadata { internalRepr: Map(0) {}, options: {} }
}

But, when I change my hander to the following:

async function getUserFullName(ctx) {
  ctx.res = (async () => {
    const user = await UserModel.findById(ctx.req.userId);
    return { fullName: `${user.firstName} ${user.lastName}` };
  })()
}

It works fine, but I'm not sure why. Is it due to the UNARY nature of the call?

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

1 participant