[SOLVED] Retrieving child object not possible

Hey guys,

In my Moralis Database I have an object called Form. This object has a column called Owner. This Owner column is a relation to the User table. Each Form row has one Owner attached.

The problem is that I can’t fetch the Owner object. I can perfectly fetch each Form I need, and I can read and retrieve all properties of the Form, just not the Owner (User) object.

I know I need to manually fetch it using:

owner = form.get(“owner”);
owner.fetch().then((owner) => {
…etc.

However, it (Nodejs console) gives me an error: cannot read object owner.

What am I doing wrong? Each Form should have just one Owner (User), should I use Pointers instead of Relation field type? Or should I fetch the Owner differently?

Ultimately, I want to be able to read the username, emailaddress fields etc. of the Owner (User) of the Form.

Thank you!
Roel

if you run that in local node, then you could also connect directly to mongo db to get the data

you could use query.include and query.select to include the pointer data in the select result

Hi @cryptokid,

Thanks for your response!. I’m not sure this answers all my questions though.

  • Why am I getting those errors?
  • Should I use a Pointer as the Owner instead of a Relation field type?
  • Is having my Form object already available not enough to get the Owner (User) object attached to that Form object?

Thanks again!
Roel

it may be easier with pointers, I don’t know the answer to all those questions

Gotcha, I’ll take a look at those to see if they will work in my application/script structure. Thanks again!

you can also search on google in general question on how to do it with parse server as Moralis Server uses parse server

Hi,

I would still like to get advice about this. Whatever I do, I can’t seem to get the user object that I added as a pointer to an Object.

Parent object: Form.
Pointer column inside Form; “FormOwner” (points to the User table)

If I query a Form, I get all its attributes, including the FormOwner. But of this FormOwner object I can only access the id field, and nothing else (console.log(FormOwner.id) gives me the expected id of the User record I’m trying to fetch). I can however not access any other attributes.

You would think that you could query the User table using the id of the user that I now have, but this always returns zero results ([]);

The Form object looks like this when fetched and logging to console:

ParseObjectSubclass {
  className: 'Form',
  _objCount: 1,
  id: 'ym5o8jomA7FutCmI3SiR8JXe'
}

The FormOwner (User) object (accessed with const FormOwner = await form.get(“FormOwner”);), looks like this:

ParseUser {
  id: 'H9sfhdhyukNrhOytYDoUaAfe',
  _localId: undefined,
  _objCount: 2,
  className: '_User'
}

console.log(FormOwner.id) gives the expected id.
console.log(FormOwner.username) however gives me undefined.

If I try to do await console.log(FormOwner.get(“username”)); it also returns undefined.

If I query the User table based on the id value that I have, it returns the following:

const Owner  = Moralis.Object.extend("User");
const query = new Moralis.Query(Owner);
query.equalTo("objectId", FormOwner.id);
const results = await query.find();
console.log(results);

Returns: [] (empty array)…

What on Earth am I doing wrong? This is all running on a local nodejs server. Thanks!!!

Usually in order to do a find on user table you will need to use master key

And for pointers in particular you may need to use query.include and query.select syntax to also include the pointer object and not only the pointer with the object id

I found the issue…

As we’re running the Moralis SDK on our own Nodejs server, we had to explicitly define and provide the master key during Moralis initialization, like so:

const masterKey = “MASTER_KEY_HERE”;
Moralis.start({ serverUrl, appId, masterKey });

This allowed the use of the useMasterKey:true option. Notice that initializing Moralis without the masterKey option still allowed us to use useMasterKey:true without receiving any errors or warnings. Not sure if that’s the perfect scenario.

Either way, this way it worked! :slight_smile: Thanks for pointing me in the right direction.

1 Like