英文:
Firestore query array
问题
如果我删除 `.whereArrayContainsAny("ItemID", Arrays.asList(onCart))` 这个语句,就没有错误,它会检索集合中的所有文档,但这不是我想要的,我只想检索具有与 onCart 列表中的 `ItemID` 值相匹配的文档。`onCart` 是来自前一个活动意图的。
> 错误消息:java.lang.IllegalArgumentException: 无效数据。不支持嵌套数组"
@Override
protected void onStart() {
super.onStart();
List<String> onCart = getIncomingIntent();
loadCart(onCart);
}
private void loadCart(List<String> onCart) {
db.collection("Items")
.whereArrayContainsAny("ItemID", Arrays.asList(onCart))
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
Log.d(TAG, document.getId() + " => " + document.getData());
}
} else {
Log.d(TAG, "获取文档时出错:", task.getException());
}
}
});
}
private List<String> getIncomingIntent() {
if (getIntent().hasExtra("checkOut")) {
return getIntent().getStringArrayListExtra("checkOut");
}
return null;
}}
请注意,这段代码中包含 HTML 编码(如 "
)。如果您需要在实际代码中使用这些代码片段,请将 HTML 编码还原为正常的双引号。
英文:
If I remove the .whereArrayContainsAny("ItemID", Arrays.asList(onCart))
the statement, there is no error and it retrieves all the documents in a collection, but that is not what I want, I want to retrieve only the documents with ItemID
value that is also in the onCart list onCart
is from the intent of the prev activity.
> error msg: java.lang.IllegalArgumentException: Invalid data. Nested arrays are not supported"
@Override
protected void onStart() {
super.onStart();
List<String> onCart=getIncomingIntent();
loadCart(onCart);
}
private void loadCart(List<String> onCart) {
db.collection("Items")
.whereArrayContainsAny("ItemID", Arrays.asList(onCart))
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
Log.d(TAG, document.getId() + " => " + document.getData());
}
} else {
Log.d(TAG, "Error getting documents: ", task.getException());
}
}
});
}
private List<String> getIncomingIntent() {
if (getIntent().hasExtra("checkOut")) {
return getIntent().getStringArrayListExtra("checkOut");
}
return null;
}}
答案1
得分: 1
"If i remove the .whereArrayContainsAny("ItemID", Arrays.asList(onCart)) statement, there is no error and it retrieves all the documents in a collection
That's the expected behavior since calling .collection("Items")
along with .get()
will return all documents within Items
collection. However, when adding a call to:
.whereArrayContainsAny("ItemID", Arrays.asList(onCart))
It means that you are looking for every document where the ItemID
field is an array that contains one of the elements that exist in the onCart
array. This is actually no possible because your ItemID
property is a String and not an array:
See the double quotes? If you need the functionality that the whereArrayContainsAny()
method provides, you should change your ItemID
property to be of type array
. If you only want to check the values of your onCart
array against your String property ItemID
, then you should simply use:
.whereIn("ItemID", Arrays.asList(onCart));
This query returns every document where the ItemID
field is set to one of the elements that exist in the onCart
array."
英文:
> If i remove the .whereArrayContainsAny("ItemID", Arrays.asList(onCart)) statement, there is no error and it retrieves all the documents in a collection
That's the expected behavior since calling .collection("Items")
along with .get()
will return all documents within Items
collection. However, when adding a call to:
.whereArrayContainsAny("ItemID", Arrays.asList(onCart))
It means that you are looking for every document where the ItemID
field is an array that contains one of the elements that exist in the onCart
array. This is actually no possible because your ItemID
property is a String and not an array:
See the double quotes? If you need the functionality that the whereArrayContainsAny()
method provides, you should change your ItemID
property to be of type array
. If you only want to check the values of your onCart
array against your String property ItemID
, then you should simply use:
.whereIn("ItemID", Arrays.asList(onCart));
This query returns every document where the ItemID
field is set to one of the elements that exist in the onCart
array.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论