Protocol Buffer - 分配嵌套消息
Protocol Buffer - assign nested message
我的 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
是空的。
如何为嵌套消息正确赋值?
您定义了一条消息,但没有声明该消息类型的字段。我想你的意思是
message ProductRecommendationReply {
string productid = 1;
message productrecommendationlist {
string recommendedproductid = 1;
string recommendedproductname = 2;
string recommendedproductprice = 3;
}
repeated productrecommendationlist recommendationlist = 2;
}
我的 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
是空的。
如何为嵌套消息正确赋值?
您定义了一条消息,但没有声明该消息类型的字段。我想你的意思是
message ProductRecommendationReply {
string productid = 1;
message productrecommendationlist {
string recommendedproductid = 1;
string recommendedproductname = 2;
string recommendedproductprice = 3;
}
repeated productrecommendationlist recommendationlist = 2;
}