协议缓冲区-分配嵌套消息

时间:2020-02-03 13:20:50

标签: javascript protocol-buffers grpc

我的PB模式如下:

message ProductRecommendationReply {
  string productid = 1;
  message recommendationlist{
    string recommendedproductid = 1;
    string recommendedproductname = 2;
    string recommendedproductprice = 3;
  }
}

在服务器端(node.js),我试图设置如下属性:

var message = {
            productid: '',
            recommendationlist: []
        };
        message.productid = result_list.product_Id;
        message.recommendationlist.recommendedproductid = result_list.recomendation_list[0].recommended_product_id;
        message.recommendationlist.recommendedproductname = result_list.recomendation_list[0].recommended_product_name;
        message.recommendationlist.recommendedproductprice = result_list.recomendation_list[0].recommended_product_price;

        callback(null,message); // send the result back to consumer.

问题是调试客户端时,只有productid分配了一个值,recommendationlist为空。

如何为嵌套消息正确分配一个值?

1 个答案:

答案 0 :(得分:1)

您定义了一条消息,但未声明该消息类型的字段。我认为您是要这样做

message ProductRecommendationReply {
  string productid = 1;
  message productrecommendationlist {
    string recommendedproductid = 1;
    string recommendedproductname = 2;
    string recommendedproductprice = 3;
  }
  repeated productrecommendationlist recommendationlist = 2;
}