如何使用 grpc nodejs 返回待办事项列表,结果为空:
但是,当我在没有 grpc 的情况下尝试它时,它会返回带有数据的列表,但是当我尝试使用 grpc 获取待办事项列表时,它会返回空结果{ todo: [] }
注意:其他功能,如 Insert Update Delete 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.
.原始文件
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;
}