[SOLVED] Aggregate call requires masterKey

I’m executing query.aggregate using moralis/node and it triggers an error due to missing masterkey.

What is the correct way to call aggregate with the master key?

did you add masterKey in Moralis.start?

Yes, but additionally it’s required to pass it on any subsequent call such as .count or .find by passing the option dictionary with {useMasterKey: true}, for .aggregate this option dictionary is not available somehow.

what you mean by this?

I can’t pass const opts={useMasterKey: true} to a query.aggregate(aggregatePipeline, opts) call

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?