英文:
FireStore Data insert (Android Studio 3.6.1)
问题
我想在我的FireStore数据库中创建新文档并从我的应用程序中插入评论。当我尝试调试我的代码以进行评论插入时,我发现onSuccess方法被跳过,在toAdd(String, 这是评论)不为空的情况下,代码运行onFailure方法,我无法理解为什么。这是我的代码:
public void insertComment(View v) {
toAdd = comment.getText().toString();
if (!toAdd.isEmpty()) {
//TODO : 检查这个解决方案(onSuccess不起作用)
String currentUser = FirebaseAuth.getInstance().getCurrentUser().getUid();
Map<String, Object> comment = new HashMap<>();
comment.put(COMMENT, toAdd);
db.collection("Users").document(currentUser + " Comments").set(comment)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
counter++;
ListItem listItem = new ListItem(counter, toAdd);
listItems.add(listItem);
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
Toast.makeText(ListActivity.this, "评论已保存", Toast.LENGTH_SHORT).show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(ListActivity.this, "错误!", Toast.LENGTH_SHORT).show();
}
});
}
else {
Toast.makeText(ListActivity.this, "空评论!", Toast.LENGTH_SHORT);
}
}
这是您提供的代码的翻译部分。
英文:
I want to create new document in my DB in FireStore and insert a comment from my app, When I tried to debug My code in case of comment insert, I found
that onSuccess method is skipped, in every case when toAdd(String, this is the comment) isn't empty The
code runs onFailure Method, and I can't understand why. Here is my code:
public void insertComment(View v) {
toAdd = comment.getText().toString();
if (!toAdd.isEmpty()) {
//TODO : CHECK THIS SOLUTION (onSuccess not working)
String currentUser = FirebaseAuth.getInstance().getCurrentUser().getUid();
Map<String, Object> comment = new HashMap<>();
comment.put(COMMENT, toAdd);
db.collection("Users").document(currentUser+" Comments").set(comment)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
counter++;
ListItem listItem = new ListItem(counter, toAdd);
listItems.add(listItem);
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
Toast.makeText(ListActivity.this, "Comment saved", Toast.LENGTH_SHORT).show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(ListActivity.this, "Error!", Toast.LENGTH_SHORT).show();
}
});
}
else {
Toast.makeText(ListActivity.this, "Empty Comment!", Toast.LENGTH_SHORT);
}
}
答案1
得分: 0
通过数据库解决 -> 规则 ->
将 allow read, write: if false; 更改为 true;
英文:
Solve it via Database -> Rules ->
Change allow read, write: if false; to true;
专注分享java语言的经验与见解,让所有开发者获益!
评论