我在firestore中的集合中有Array对象。当我调用集合(Orders)来获得单个订单时,它会正确地检索所有对于名为“食品”的对象Array的期望。
参考订单
mOrderRef = mFirestore.collection("orders").document(orderId);
onEvent功能
@Override
public void onEvent(DocumentSnapshot documentSnapshot,
FirebaseFirestoreException e) {
if (e != null) {
Log.w(TAG, "order:onEvent", e);
return;
}
onOrderLoaded(documentSnapshot.toObject(Order.class));
}
onOrderLoaded函数
private void onOrderLoaded(Order order) {
Log.d(TAG, String.valueOf(order.getMenu())); // why is printing null?
}
这是订单类的完整代码
@IgnoreExtraProperties
public class Order {
public static final String FIELD_USER_NAME = "username";
public static final String FIELD_USER_PHONE = "user_phone_number";
public static final String FIELD_USER_ID = "user_id";
public static final String FIELD_RESTAURANT_NAME = "restaurant_name";
public static final String FIELD_RESTAURANT_ID = "restaurant_id";
public static final String FIELD_STATUS = "status";
public static final String FIELD_SUBMITTED_AT = "submitted_at";
public static final String FIELD_TOTAL_COST = "total_cost";
public static final String FIELD_MENU = "foods";
private String username;
private String user_phone_number;
private String user_id;
private String restaurant_id;
private String restaurant_name;
private String status;
private Date submitted_at;
private Integer total_cost;
private Menu[] foods;
public Order() {}
public Order(String user_name, String user_phone_number, String user_id, String restaurant_id, String restaurant_name, String status, Date submitted_at, Integer total_cost, Menu[] foods) {
this.username = user_name;
this.user_phone_number = user_phone_number;
this.user_id = user_id;
this.restaurant_id = restaurant_id;
this.restaurant_name = restaurant_name;
this.status = status;
this.submitted_at = submitted_at;
this.total_cost = total_cost;
this.foods = foods;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUser_phone_number() {
return user_phone_number;
}
public void setUser_phone_number(String user_phone_number) {
this.user_phone_number = user_phone_number;
}
public String getUser_id() {
return user_id;
}
public void setUser_id(String user_id) {
this.user_id = user_id;
}
public String getRestaurant_id() {
return restaurant_id;
}
public void setRestaurant_id(String restaurant_id) {
this.restaurant_id = restaurant_id;
}
public String getRestaurant_name() {
return restaurant_name;
}
public void setRestaurant_name(String restaurant_name) {
this.restaurant_name = restaurant_name;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Date getSubmitted_at() {
return submitted_at;
}
public void setSubmitted_at(Date submitted_at) {
this.submitted_at = submitted_at;
}
public Integer getTotal_cost() {
return total_cost;
}
public void setTotal_cost(Integer total_cost) {
this.total_cost = total_cost;
}
public Menu[] getMenu() {
return foods;
}
public void setMenu(Menu[] foods) {
this.foods = foods;
}
}
菜单型号
public class Menu {
public static final String FIELD_ID = "id";
public static final String FIELD_NAME = "name";
public static final String FIELD_QUANTITY = "quantity";
public static final String FIELD_PRICE = "price";
private String id;
private String name;
private String quantity;
private String price;
public Menu() {}
public Menu(String id, String name, String quantity, String price) {
this.id = id;
this.name = name;
this.quantity = quantity;
this.price = price;
}
// Getter and Setter
}
我在firestore上的Order集合中有字段Array对象,问题是如何映射Menu模型列表?
答案 0 :(得分:0)
没有您的数据库规则或文档创建代码,很难说,但您的Menu模型似乎存储为Order模型的子集合。 FireStore only performs "shallow" queries因此您需要执行其他查询以获取特定订单文档的Menu子集的所有文档。