如何 return 来自 grpc mongoose nodejs 的结果列表?
how to return list of result from grpc mongoose nodejs?
如何使用 grpc nodejs return todos 列表,结果为空:
然而,当我在没有 grpc 的情况下尝试它时,它是 returning 带有数据的列表但是当我试图使用 grpc 获取待办事项列表时,它是 returning 空结果 { 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;
}
这里的问题是你的 TodoDb.list
函数输出一个 Todo
对象的列表,但是你正在使用的 gRPC API 需要一个 TodoList
对象,它是具有映射到该列表的键 todo
的对象。因此,您需要添加自己的回调,将列表放入这样的对象中。
在您的服务器代码中,list
函数应如下所示:
function(_, callback) {
TodoDb.list(function(error, todoList) {
if (error) {
callback(error);
} else {
// Wrap the list in an object to match TodoList message structure
callback(null, {todo: todoList});
}
}
}
如何使用 grpc nodejs return todos 列表,结果为空:
然而,当我在没有 grpc 的情况下尝试它时,它是 returning 带有数据的列表但是当我试图使用 grpc 获取待办事项列表时,它是 returning 空结果 { 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;
}
这里的问题是你的 TodoDb.list
函数输出一个 Todo
对象的列表,但是你正在使用的 gRPC API 需要一个 TodoList
对象,它是具有映射到该列表的键 todo
的对象。因此,您需要添加自己的回调,将列表放入这样的对象中。
在您的服务器代码中,list
函数应如下所示:
function(_, callback) {
TodoDb.list(function(error, todoList) {
if (error) {
callback(error);
} else {
// Wrap the list in an object to match TodoList message structure
callback(null, {todo: todoList});
}
}
}