下面是我的示例API响应,它是一个对象数组。我正尝试使用以下步骤在protobuff中转换此响应。但它总是给我空洞的回应。
[
{
travels: "Kallada Travels (Suresh Kallada)",
busType: "Volvo A/C MultiAxle Semisleeper (2+2)",
source: "Angamaly",
opId: "0003",
image_url: null,
ratings: {},
approvedImageCount: 0,
images: []
}
]
步骤:
protobuf.load("common/protos/"+modelName+".proto",function(err,root){
if(err) {
return next()
}
var messageType = codeConfig['protoFileMapping'][modelName][reqPath];
console.log('messageType',messageType)
var protoMessage = root.lookupType(messageType);
// console.log('protoMessage',protoMessage)
var jsonResp = JSON.parse(JSON.stringify(ctx.result));
console.log('jsonResp',jsonResp);
var errMsg = protoMessage.verify(jsonResp);
console.log('errMsg',errMsg)
if (errMsg){
return next();
}
//problem starts here
var msg = protoMessage.create(jsonResp);
console.log('msg',msg)
var buffer = protoMessage.encode(msg).finish();
console.log('buffer',buffer)
控制台日志中的消息如下:
msg getReviewsResp {
'0':
{ travels: 'Kallada Travels (Suresh Kallada)',
busType: 'Volvo A/C MultiAxle Semisleeper (2+2)',
source: 'Angamaly',
opId: '0003',
image_url: null,
ratings: {},
approvedImageCount: 0,
images: [] },
response: [] }
在理想情况下,msg应与API响应相同,缓冲区不应为空。
buffer <Buffer >