gpt4 book ai didi

java - Cloud Firestore 无法识别字段名称更改

转载 作者:行者123 更新时间:2023-12-02 10:35:11 24 4
gpt4 key购买 nike

我正在同时写入 Firebase 实时数据库和 Cloud Firestore。

最初,我的 POJO 名称中有一个字段“user_id”。我最近在 POJO 中更改为“creator_id”,当我将带有修改后的字段的 POJO 写入数据库时​​,它会以“creator_id”的形式出现。但是,它仍然作为“user_id”写入 Firestore。不确定我的代码是否有问题:

mBaseRef.updateChildren(childUpdates);
Collections.reverse(mPollAnswers);
for (int i = 0; i < mPollAnswers.size(); i++) {
mBaseRef.child("Polls").child(key).child("answers").child(String.valueOf(i + 1)).updateChildren(poll.answerConvert(mPollAnswers, i));
}

String userID = FirebaseAuth.getInstance().getCurrentUser().getUid();
Log.v("USER_ID", userID);
mUserRef.child(userID).updateChildren(childUpdates);


mStoreBaseRef.collection(POLLS).add(poll).addOnCompleteListener(new OnCompleteListener<DocumentReference>() {
@Override
public void onComplete(@NonNull Task<DocumentReference> task) {
if (task.isSuccessful()) {
DocumentReference docRef = task.getResult();
String key = docRef.getId();
Log.v("KEY", key);
Toast.makeText(getApplicationContext(), key, Toast.LENGTH_LONG).show();
CollectionReference pollAnswerRef = mStoreBaseRef.collection("Polls").document(key).collection("answers");
//TODO: need to add answers
for (int i = 0; i < mPollAnswers.size(); i++){
pollAnswerRef.document(String.valueOf(i + 1)).set((poll.answerConvert(mPollAnswers, i)), SetOptions.merge());
}

}
}

});

POJO:

@IgnoreExtraProperties
public class Poll {

private String question;
private String image_URL;
private String creator_ID;
private String display_name;
private int vote_count;
private Long epoch;
private long trend_score;

@ServerTimestamp
private Date date;

private ArrayList<String> answers;

public Poll() {
}

public Poll(String Question, String Image_URL, ArrayList<String> answers, int vote_count, String creator_ID, String DisplayName, Long epoch, long trend_score) {
this.question = Question;
this.image_URL = Image_URL;
this.answers = answers;
this.vote_count = vote_count;
this.creator_ID = creator_ID;
this.display_name = DisplayName;
this.epoch = epoch;
this.trend_score = trend_score;
this.date = date;
}

public String getUser_id() {
return creator_ID;
}

public String getDisplay_name() {
return display_name;
}

public String getQuestion() {
return question;
}

public void setQuestion(String question) {
this.question = question;
}

public String getImage_URL() {
return image_URL;
}

public Long getEpoch() {
return epoch;
}

public void setEpoch(Long epoch) {
this.epoch = epoch;
}

public void setUser_id(String user_id) {
this.creator_ID = user_id;
}

public void setDisplay_name(String display_name) {
this.display_name = display_name;
}

public void setImage_URL(String image_URL) {
this.image_URL = image_URL;
}

public Integer getVote_count() {
return vote_count;
}

public void setVote_count(Integer vote_count) {
this.vote_count = vote_count;
}

public long getTrend_score() {
return trend_score;
}

public void setTrend_score(long trend_score) {
this.trend_score = trend_score;
}

@Exclude
public Map<String, Object> toMap() {
HashMap<String, Object> result = new HashMap<>();
result.put("question", question);
result.put("image_URL", image_URL);
result.put("vote_count", 0);
result.put("creator_ID", creator_ID);
result.put("display_name", display_name);
result.put("epoch", epoch);
result.put("trend_score", trend_score);
return result;
}

@Exclude
public Map<String, Object> answerConvert(ArrayList<String> answers, int index){
HashMap<String, Object> result = new HashMap<>();
result.put("answer", answers.get(index));
result.put("vote_count", 0);
}

}

最佳答案

根据 JavaBeans 规范,当底层成员是私有(private)成员时,getter 的名称将用作派生字段/属性的名称的基础。所以这个:

private String creator_ID;

public String getUser_id() {
return creator_ID;
}

实际上是告诉系统您想要读取和写入一个名为user_id而不是creator_ID的字段。您应该更改 getter 和 setter 的名称,以匹配您希望数据库中的字段名称。

关于java - Cloud Firestore 无法识别字段名称更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53347447/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com