使用Node JS将对象数组映射到Protobuf时清空响应

时间:2018-06-18 09:50:04

标签: javascript node.js protocol-buffers protobuf-net protobuf.js

下面是我的示例API响应,它是一个对象数组。我试图使用以下步骤在protobuff中转换此响应。但它始终给我空洞的回应。

public static void Seed(IServiceProvider applicationBuilder)
        {
            AppDbContext context =
                applicationBuilder.GetRequiredService<AppDbContext>();
        }

步骤:

    [
    {
    travels: "Kallada Travels (Suresh Kallada)",
    busType: "Volvo A/C MultiAxle Semisleeper (2+2)",
    source: "Angamaly",
    opId: "0003",
    image_url: null,
    ratings: {},
    approvedImageCount: 0,
    images: []
    }
   ]

响应:

控制台日志中的msg如下所示:

    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应与API响应相同,缓冲区不应为空。

   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: [] }

0 个答案:

没有答案