Moralis.Object to JSON

I ran into the problem of parsing Moralis Objects in the context of Next.Js getStaticProps from serverside rendering to the client context.

The parse-server has the option to convert queries with its query.toJson()method. Is there a similar implementation available under theMoralis.Object` ?

I didn’t understand the question, can you give some examples?

I’m computing database entries for static page generation and fetch them server side using the masterKey.

These computed entries are then serialised as JSON and sent to the client combined as HTML.

The error message is
SerializableError: Error serializing `.variableName[0]` returned from `getStaticProps` in "/".

where variableName = await query.find({useMasterKey: true});

maybe that object is not json serialisable, you could try JSON.stringify(variableName)

1 Like

That’s weird, that seems to work.

I now created a new array and iterate over each query array entry and call JSON.stringify. It worked well.

The previous error message also said

Reason: `object` ("[object Object]") cannot be serialized as JSON. Please only return JSON serializable data types.

but it’s exactly this object that I call JSON.stringify on :thinking:

Well, you found a solution already! Still curious in case you have a pointer why that failed before. :eyes:

Related to this topic, I noticed that it serialises all Moralis.Object properties such as object id etc.

I looked into the Mongo aggregation and saw some examples for pipelines returning only the desired fields of a subfield, but I’m struggeling with the generalisation for the array of pointers.

Let’s say I only want to retrieve an array of accounts.get(“someAttribute”) in the query

const query = new Moralis.Query("SomeCollection");
const accounts = query[0].get("accounts");
const desiredArray = accounts.map(account=>account.get("someAttribute"));

Is there a way so that my accounts is already the desiredArray ?

I don’t know to answer this question, you should create different forum threads for different questions

Will do. Thanks for the quick feedback!