如何从grpc猫鼬nodejs返回结果列表?

时间:2018-09-20 18:10:37

标签: node.js grpc grpc-node grpc-web

如何使用grpc nodejs返回待办事项列表,结果为空:

但是,当我尝试不使用grpc时,它会返回包含数据的列表,但是当我尝试使用grpc来获取待办事项列表时,它会返回空结果{ todo: [] } 注意:其他功能(如“插入更新”,“删除ReadOne”)运行正常。仅未获取所有待办事项列表

最后没有给出gRPC的工作代码

这是我的gRPC服务器

const PROTO_PATH = __dirname + '../../../protos/todo.proto';
const grpc = require('grpc');
var protoLoader = require('@grpc/proto-loader');

var packageDefinition = protoLoader.loadSync(PROTO_PATH, { keepCase: true, oneofs: true });
var todoproto = grpc.loadPackageDefinition(packageDefinition).todoproto;
const server = new grpc.Server();


server.addService(todoproto.TodoService.service, {

    list: function (_, callback) {
        TodoDb.list(callback);
    },
});

// gRPC Server
server.bind('0.0.0.0:50051', grpc.ServerCredentials.createInsecure());
console.log('grpc server starting on :', '0.0.0.0:50051');
server.start();
console.log('grpc server running on :', '0.0.0.0:50051');

这是我的gRPC客户端

const PROTO_PATH = __dirname + '../../../../protos/todo.proto';
const grpc = require('grpc');
var protoLoader = require('@grpc/proto-loader');

var packageDefinition = protoLoader.loadSync(
    PROTO_PATH, {
        keepCase: true, enums: String, defaults: true, arrays: true, objects: true, oneofs: true
    });
var todoproto = grpc.loadPackageDefinition(packageDefinition).todoproto;
// The protoDescriptor object has the full package hierarchy

var client = new todoproto.TodoService('0.0.0.0:50051', grpc.credentials.createInsecure());

try {
    client.list({}, (error, todo) => {
        console.log(todo);
        if (todo) { console.log('Passed.') }
    });
} catch (error) {
    console.log(error.message);
}

var command = process.argv.shift();

if (command == 'list')
    todosList();

工作测试代码

var assert = {
    list: function (_, callback) {
        TodoDb.list(callback);
    },
};

try {
    assert.list({}, (error, todo) => {
        console.log(todo);
        if (todo) { console.log('Passed.') }S
    });
} catch (error) {
    console.log(error.message);
}

未通过测试的代码

[ { done: false,
    createdate: 2018-09-16T14:46:43.855Z,
    id: 6,
    title: 'Todo App',
    description: 'Complete project Before 22 September' },
  { done: false,
    createdate: 2018-09-16T14:46:43.855Z,
    id: 7,
    title: 'Todo App',
    description: 'Complete project Before 22 September' },
  { done: false,
    createdate: 2018-09-16T14:46:43.855Z,
    id: 5,
    title: 'Todo App',
    description: 'Complete project Before 22 September' } ]
Passed.

.Proto File

syntax = "proto3"; //Using proto3 version.

package todoproto; 

service TodoService {
  rpc List (Empty) returns (TodoList){}
  rpc Insert (Todo) returns (Empty){}
  rpc Get (TodoId) returns (Todo){}
  rpc Update (Todo) returns (Todo){}
  rpc Delete (TodoId) returns (Empty){}
}

message Empty {}

message Todo {
  int32 id = 1;
  string title = 2;
  string description = 3;
}

message TodoList {
  repeated Todo todo = 1;
}

message TodoId {
  int32 id = 1;
}

1 个答案:

答案 0 :(得分:1)

这里的问题是您的TodoDb.list函数输出一个Todo对象的列表,但是您使用的gRPC API期望一个TodoList对象,该对象是键为{ {1}}映射到该列表。因此,您需要添加自己的回调,将列表放入类似的对象中。

在服务器代码中,todo函数应如下所示:

list