Untitled

mail@pastecode.io avatar
unknown
javascript
2 years ago
1.6 kB
2
Indexable
Never

  useEffect(() => {
    const getContacts = async() => {
      // defining query instances
      const Contacts = Moralis.Object.extend("Contacts");
      const queryContacts = new Moralis.Query(Contacts);
      const Addresses = Moralis.Object.extend("Addresses");
      const queryAddress = new Moralis.Query(Addresses);

      // getting logged in user
      const user = Moralis.User.current();
      
      // setting the pointer to the user
      queryContacts.equalTo('owner', user);
      const contactResults = await queryContacts.find();

      // create query constraints-pointing to the contact who owns the address/es
      queryAddress.equalTo('owner', {
        __type: 'Pointer',
        className: 'Contacts',
        objectId: contactResults.id // I think I need to somehow loop these ids
      });

      // filter columns
      queryAddress.select('address', 'network', 'notes');
      const addressResults = await queryAddress.find()

      let results =[]
      if (contactResults) {
        // for (let i = 0; i < addressResults.length; i++) - Tried looping through addresses.
        for (let i = 0; i < contactResults.length; i++)
        results.push({
          name: contactResults[i].attributes?.name,
          contactCards: {
            address: addressResults.attributes?.address,
            network: addressResults.attributes?.network,
            notes: addressResults.attributes?.notes,
          }
        })
        console.log(results)
      }
      }
      return () => {
        getContacts()
      }
  }, [])