[SOLVED] Aggregate call requires masterKey

this doesn’t work: const result = await query.aggregate(pipeline, {useMasterKey: true}); ?

I get

error - ParseError: unauthorized: master key is required

when I call (after the usual Moralis.start and other queries work nicely)

    const pipeline = [
        {match: {day: 190}} //{gt: 190, lt: 202}}}
    ]

    return await query.aggregate(pipeline, {useMasterKey: true})// , {useMasterKey: true})

Edit: Note my first post that I’m using import Moralis from "moralis/node". Is it maybe not ported to the server side backend implementation for node?

did you add the master key to Moralis.start ?

yes I did add it to moralis.Start (Note that other queries work, also typescript does not type hint the option key)

and if you use that syntax from a cloud function, I guess that it works

1 Like

It seems that I can’t use Moralis.Cloud.define from a moralis/node context. For prototyping a bit cumbersome if I have to move my code definition in another location.

I can try it with the client side of things for the sake of trying, but from what I understood with Cloud code from the parse-server it should work since it defaults to using the master key.

you still need to use that syntax with master key as parameter in cloud code

moralis/node has this signature for query.aggregate
aggregate<V = any>(pipeline: Query.AggregationOptions | Query.AggregationOptions[]): Promise<V>;

and AggregationOptions is missing the Scopeoptions which includes interface ScopeOptions extends SessionTokenOption, UseMasterKeyOption {}

So it seems it never is included there. I assume it was forgotten to update? :thinking:

Yes I also think that it is still necessary to use the option in the Moralis.Cloud context, I’m however not very eager to move to Moralis.Cloud just for that function. I’m now making a more expensive server side computation on my end instead. Would have been nice to use the aggregation pipeline. Well, hopefully in a future release it will be available.

you could call a cloud function with specific parameters and get the result back, as a workaround for now

Please see above post that mentions the lack of Moralis.Cloud.define in the moralis/node.

I would have to create a client side admin page to maintain or update these sort of functions or define it without version control in the console/Moralis dashboard, which seems very cumbersome for my code base. It’s not really an attractive work around for my situation. I explored it a bit before and appreciate the recommendation though :+1:

I still don’t understand what this means

you can create a cloud function, upload it to a Moralis Server, and call it there, or you can connect directly to mongo db and run the query there

TypeError: moralis_node__WEBPACK_IMPORTED_MODULE_1___default(...).Cloud.define is not a function

yes, I don’t expect that to work, I didn’t want to say to use that on your server

Yes, and I’m saying that I want to contain my code base under version control in the vicinity of feature development. I don’t want to manually move to another admin page or use the moralis dashboard manually and update the query whenever I change something :+1:

Again, thank you for the recommendation, it just isn’t the right fit for my situation.

accessing mongo db directly doesn’t work for you?

There was the problem with whitelisting IPs, since I have a server that rotates IP addresses, so the automatically regenerating backend will at some point not be able to connect to the database since I can’t whitelist a DNS address.

can you give a minimal complete code example with .aggregate that I could also test if it works?


import Moralis from `moralis/node`

const testMoralis = async () => {
Moralis.start({
appId: ...
serverUrl: ...
masterKey: ...
})
const pipeline = [
{match: {someField: "fieldValue"}}
]
const result = await (new Moralis.Query("SomeCollection")).aggregate(pipeline, {useMasterKey: true})
} 

Something like this

this works fine for me:


x = async () => {
    const  Moralis = require('moralis/node')
    console.log(Moralis.CoreManager.get("VERSION"))
    await Moralis.start({ serverUrl: "https://afasdf:2053/server", appId: "sdgasdg", masterKey: "sfgsdfg" });

    const pipeline = [
        {match: {username: "Gn6Px7RUd9JkiJLVTdEMD4cen"}}
    ]
    const result = await (new Moralis.Query("User")).aggregate(pipeline, {useMasterKey: true})
    console.log(result);

    }

x();

1 Like

It seems that the queries I made before were all on default public fields so they never triggered this error. I had multiple Moralis initialiser definitions with different environment variables for each different test server. The masterkey was passed to the Moralis.start in this case, but it was a copy paste error from another server`s initialiser that only had the correct URL and app id.

Thanks for your help in resolving this. Much appreciated!

1 Like