Firestore 数据插入(Android Studio 3.6.1)

huangapple 未分类评论44阅读模式
英文:

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&lt;String, Object&gt; comment = new HashMap&lt;&gt;();

        comment.put(COMMENT, toAdd);

        db.collection(&quot;Users&quot;).document(currentUser+&quot; Comments&quot;).set(comment)
                .addOnSuccessListener(new OnSuccessListener&lt;Void&gt;() {
                    @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, &quot;Comment saved&quot;, Toast.LENGTH_SHORT).show();
                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Toast.makeText(ListActivity.this, &quot;Error!&quot;, Toast.LENGTH_SHORT).show();
                    }
                });
    }

    else {
        Toast.makeText(ListActivity.this, &quot;Empty Comment!&quot;, Toast.LENGTH_SHORT);
    }
}

答案1

得分: 0

通过数据库解决 -> 规则 ->

将 allow read, write: if false; 更改为 true;

英文:

Solve it via Database -> Rules ->

Change allow read, write: if false; to true;

huangapple
  • 本文由 发表于 2020年4月8日 23:43:44
  • 转载请务必保留本文链接:https://java.coder-hub.com/61104637.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定