Firebase — 文档引用必须具有偶数段,但 “Rezepte” 有1段

huangapple go评论78阅读模式
英文:

Firebase -- Document references must have an even number of segments, but Rezepte has 1

问题

好的,以下是你提供的代码的中文翻译部分:


好的,所以我生成了一个随机的文档名称,现在我正在尝试从该文档中获取数据。

public void getrandomname() {
    namereference
        .get()
        .addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
            @Override
            public void onSuccess(DocumentSnapshot documentSnapshot) {
                if (documentSnapshot.exists()) {
                    String names = documentSnapshot.get("dokumentnamen").toString().replace("[", "").replace("]", "");
                    String[] namelist = names.split(", ");
                    Random random = new Random();
                    int randomNumber = random.nextInt(namelist.length);
                    randomrezeptname = namelist[randomNumber];
                }
            }
        });
}

生成随机名称的操作正常运行,名称是存在的。
但是现在我正试图在另一个 void 中使用该字符串作为 DocumentReference,但它根本不起作用。

DocumentReference documentReference = db.collection("Rezepte").document(this.randomrezeptname);

我始终会收到相同的错误:
> 无效的文档引用。文档引用必须具有偶数个段,但 Rezepte 只有 1 个段。

我尝试过直接使用字符串的内容并将其直接放入 DocumentReference,这样可以工作。但是我必须使用一个变量,因为它应该显示一个随机文档。
你有什么想法,我做错了什么吗?

英文:

Ok so I generated a random name of a document and now I'm trying to get data from that document.

        namereference
                .get()
                .addOnSuccessListener(new OnSuccessListener&lt;DocumentSnapshot&gt;() {
                    @Override
                    public void onSuccess(DocumentSnapshot documentSnapshot) {
                        if (documentSnapshot.exists()){
                            String names = documentSnapshot.get(&quot;dokumentnamen&quot;).toString().replace(&quot;[&quot;, &quot;&quot;).replace(&quot;]&quot;, &quot;&quot;);
                            String [] namelist = names.split(&quot;, &quot;);
                            Random random = new Random();
                            int randomNumber = random.nextInt(namelist.length);
                            randomrezeptname = namelist[randomNumber];

                        }
                    }
                });
    } 

Generating a random name works fine and the name exists.
But now I'm trying to use that String in a DocumentReference within another void but it doesn't work at all.

DocumentReference documentReference = db.collection(&quot;Rezepte&quot;).document(this.randomrezeptname);

I always get the same error:
>Invalid document reference. Document references must have an even number of segments, but Rezepte has 1

I tried using the content of the String and put it directly in the DocumentReference and it worked. But I have to use a variable since It's supposed to show a random document.
Any ideas what I did wrong?

答案1

得分: 0

我猜当你说

> I tried using the content of the String and put it directly in the DocumentReference and it worked

你的意思是你运行了

DocumentReference documentReference = db.collection("Rezepte").document("a_random_recipe_name");

而且它起作用了,这表明你的层次结构是正确的。

你确定随机名称生成器在你尝试使用它来访问随机文档之前实际上设置了 randomrezeptname 吗?我的意思是,如果你运行:

1) namereference.get().addOnSuccessListener(OnSuccessListener);
2) System.out.println(this.randomrezeptname)
3) DocumentReference documentReference = db.collection("Rezepte").document(this.randomrezeptname)

第2行和第3行可能会在 OnSuccessListener 运行之前运行,这意味着在 OnSuccessListener 设置它之前,你对于 randomrezeptname 的任何奇怪行为都会出现。OnSuccessListener 在稍后的某个时间在主线程上运行,但在异步方法返回之后运行。

英文:

I'm guessing that when you say
> I tried using the content of the String and put it directly in the DocumentReference and it worked

You mean that you ran

DocumentReference documentReference = db.collection(&quot;Rezepte&quot;).document(&quot;a_random_recipe_name&quot;);

And it worked, suggesting that your hierarchy is fine.

Are you sure that the random name generator thing actually sets randomrezeptname before you try to use it to access a random document? I mean to say that if you run:

1) namereference.get().addOnSuccessListener(OnSuccessListener);
2) System.out.println(this.randomrezeptname)
3) DocumentReference documentReference = db.collection(&quot;Rezepte&quot;).document(this.randomrezeptname)

Lines 2 and 3 will probably run before OnSuccessListener is run, meaning you'll have some odd behavior around whatever randomrezeptname is before OnSuccessListener sets it. OnSuccessListener runs some time later, on the main thread, but after an asynchronous method returns.

huangapple
  • 本文由 发表于 2020年9月26日 03:28:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/64070276.html
匿名

发表评论

匿名网友

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

确定