0

My database is like this:

enter image description here

I'm trying to the for example "Ricardo Meireles" with the following code:

const getTarefasFuncionario = async () => {
    setTarefasFuncionario([]);

    const q = query(
      collection(db, "tarefas"),
      where("funcionarios", "array-contains", {
        photoURL: currentUser.photoURL,
        value: currentUser.displayName,
      })
    );

    const querySnapshot = await getDocs(q);

    const tarefas = [];
    querySnapshot.forEach((doc) => {
      tarefas.push({ ...doc.data() });
      console.log(doc.data());
    });

    tarefas.forEach((tarefa) => {
      const data = tarefa.dia.toDate();
      const options = { day: "2-digit", month: "2-digit", year: "numeric" };
      tarefa.dia = data.toLocaleDateString("pt-BR", options);
    });

    setTarefasFuncionario(tarefas);
  };

But its not getting anything, I tried to use the firebase query system like this:

enter image description here

But it also did not work, what am I doing wrong?

I'm expecting a an array of "tarefas" that contains the user as a "funcionario"

1 Answers1

1

The array-contains operation looks for an item in an array that is an exact, complete match to the value you specify. So to match the items in your array, you need to specify both the value and the photo URL. There is no operator to do a partial match on array items.

The common workaround is to add an additional array field with just the value you want to search on, e.g.

funcionarioNames: ["Ricardo Meireles", "cristano"]

With that additional field, you can now perform an array-contains on funcionarioNames.

Also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • I see what you did, I'm trying this "work around" `const q = query( collection(db, "tarefas"), where("funcionarios", "array-contains", { photoURL: currentUser.photoURL, label: currentUser.displayName, }) );` like what I saw in other link that you send me, but it doesnt work either. I'm not sure if I can use what u send because, some "tarefas" can only have one person, in that case (the print) it had like 3 users, but some other cases it would be only 1 – João Silva Mar 24 '23 at 14:45
  • I edit the code from the question with the current one – João Silva Mar 24 '23 at 14:50
  • Don't change your question in this way *after* somebody posted an answer already please, as that makes it useless to future visitors. Please revert to show the original code, and then potentially show the updated code as an edit (although that should really be a new post). – Frank van Puffelen Mar 24 '23 at 15:14