How to link/join two classes to query correct data?

What I’m trying to do is to find a way to query user-data(username, email, bio) related to the ethAddress/accounts.

Right now my user and user-data are two different classes and I’m trying to find a way to link/join them so I can query the correct user-data.

Code to set data:

//saving user data

        async function savebtn(){

            const UserData = Moralis.Object.extend("UserData");

            const currentUser = new UserData();

            currentUser.set("UserName", username.value);

            currentUser.set("Email", email.value);

            currentUser.set("Bio", bio.value);

            const fileUploadControl = document.getElementById("picfile");

            if (fileUploadControl.files.length > 0) {

                const file = fileUploadControl.files[0];

                const name = "profile_pic.jpg";

                const profilePic = new Moralis.File(name, file);

                currentUser.set('profile_pic', profilePic);

            }

            currentUser.save().then((currentUser) => {

               window.location.href = "profile.html";

            }, (error) => {

                alert('Error: ' + error.message);

            });

        }

Code to query data:

//querying userdata

            const UserData = Moralis.Object.extend("UserData");

            const query = new Moralis.Query(UserData);

            query.get(Moralis.User).then((UserData) => {

                const profilePic = UserData.get('profile_pic');

                document.getElementById('picimg').src = profilePic.url();

                const username = UserData.get('UserName');

                document.getElementById('username').innerHTML = username;

                const bio = UserData.get('Bio');

                document.getElementById('bio').innerHTML = bio;

            });

This code gets data for all users, which is not what I want to do.

So far I’ve tried adding ethAddress/accounts column in user-data but it displays an empty string with no error in console.

I’ve tried using aggregate pipeline lookup no success.

I am not that proficient in queries in general and I am going through the cloud functions and also Aggregate in detail to see what can I get. But if someone could tell me what am I doin wrong or where do I need to look that’d be super helpful. Thanks.

https://docs.moralis.io/moralis-server/database/queries#basic-queries

maybe you can do a find query:

const Monster = Moralis.Object.extend("Monster");
const query = new Moralis.Query(Monster);
query.equalTo("ownerName", "Aegon");
const results = await query.find();
alert("Successfully retrieved " + results.length + " monsters.");
// Do something with the returned Moralis.Object values
for (let i = 0; i < results.length; i++) {
  const object = results[i];
  alert(object.id + ' - ' + object.get('ownerName'));
}

Fixed it!!!..

Turns out there still alot I don’t quite know about Moralis SDK workings. I was wrong about linking classes together, it needs to be in the same row of User class as the ethAddress and accounts.

Here’s code for future readers:

Code to upload data:

async function savebtn(){

            const query = new Moralis.Query('_User');

            const results =await query.find();

            console.log(results);

            results[0].set("username", username.value);

            results[0].set("email", email.value);

            results[0].set("bio", bio.value);

            const fileUploadControl = document.getElementById("picfile");

            if (fileUploadControl.files.length > 0) {

                const file = fileUploadControl.files[0];

                const name = "profile_pic.jpg";

                const profilePic = new Moralis.File(name, file);

                results[0].set('profile_pic', profilePic);

            }

            results[0].save().then((currentUser) => {

               window.location.href = "profile.html";

            }, (error) => {

                alert('Error: ' + error.message);

            });

        }

Code to get data:

const UserData = Moralis.Object.extend(“User”);

            const query = new Moralis.Query(UserData);

            query.get(Moralis.User).then((UserData) => {

                const profilePic = UserData.get('profile_pic');

                document.getElementById('picimg').src = profilePic.url();

                const username = UserData.get('username');

                document.getElementById('username').innerHTML = username;

                const bio = UserData.get('bio');

                document.getElementById('bio').innerHTML = bio;

            });

My only feedback to Moralis is to make examples in DOCS much more elucidate and real world, meaning outside of just console.log(), I’ve spend 2 days going back and forth in DOCS and various possibilities to see how to make it work and got lucky with a YouTube video only 5mins in with some simple add ons to my code.
Anyways happy coding!!.

1 Like