【问题标题】:Protocol Buffer - assign nested message协议缓冲区 - 分配嵌套消息
【发布时间】:2020-02-03 13:20:50
【问题描述】:

我的 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.

问题是调试clinet端时,只有productid有赋值,recommendationlist是空的。

如何正确地为嵌套消息赋值?

【问题讨论】:

    标签: javascript protocol-buffers grpc


    【解决方案1】:

    您定义了一条消息,但没有声明该消息类型的字段。我想你的意思是这样做

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

    【讨论】:

    • 在“recommendationlist”上有“repeated”类型非常重要。否则数组将无法工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多