Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Protocol Buffer - assign nested message

My PB Schema looks like below:

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

At the server side (node.js), I am trying to set the properties like below:

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.

The problem is when debugging the clinet side, only the productid has a value assigned to it, the recommendationlist is empty.

How can I properly assign a value to the nested message?

like image 852
Benjamin Avatar asked Sep 02 '25 10:09

Benjamin


1 Answers

You defined a message but didn't declare a field of that message type. I think you mean to do

message ProductRecommendationReply {
  string productid = 1;
  message productrecommendationlist {
    string recommendedproductid = 1;
    string recommendedproductname = 2;
    string recommendedproductprice = 3;
  }
  repeated productrecommendationlist recommendationlist = 2;
}
like image 165
Alex Staninger Avatar answered Sep 05 '25 01:09

Alex Staninger