Query works in console, but not externally

Hello,

As the title suggests, the query runs fine in the console, but not when executed externally (through Postman).

Key Points:

  • When logged, the moviesList is empty when externally triggered, but has data when in console.
  • I have set up a default rule for the collection providing read/write access.
  • It returns empty results externally, no errors caught.

Any help is appreciated. Thanks!

// This function is the endpoint's request handler.
exports = function({ query, headers, body}, response) {
    // Data can be extracted from the request as follows:

    let qry = {}
    if(query.year) {
      qry = {"year": {$eq: parseInt(query.year)}}
      console.log("year: " + query.year);
    } else if (query.title) {
      qry = {"title": {$eq : query.title}}
      console.log("title: " + query.title);
    }
    
    // Querying a mongodb service:
    const doc = context.services.get("mongodb-atlas").db("sample_mflix").collection("movies");

    const moviesList = doc.find(qry).limit(5).toArray();
    
    
    let jsonList = JSON.stringify(moviesList);
    
    // return jsonList;
    if(response)
      response.setBody(jsonList);
      
      console.log(jsonList);
      
    return moviesList;
};

Console Output:

ran at Tue Jan 18 2022 13:17:16 GMT+0200 (Eastern European Standard Time)
took 323.632668ms
logs:
NO PARAMETERS
JSON: [{"_id":“573a1390f29313caabcd4135”,“plot” …
undefined
result:
“[{”_id":“573a1390f29313caabcd4135”,“plot” …

Not entirely certain what the “undefined” below the one console log happens to be.

@Curtis_Odorizzi Hi there,

What framework are you using?

MongoDB’s Realm app from their Cloud computing stack.

image

Sorry, I mean for the server, as setBody is not something I know of, and also I don’t know where the response is being sent to the client.

Plus you return moviesList which is rather strange.

MongoDB Realm is a serverless application backendMongoDB Realm lets you define serverless JavaScript functions called Realm Functions.

The default function it created for GET included the response argument, which looks very similar to the express route signature. I’m not familiar enough with Realm to answer you fully though… which is the reason for my posting the question here :slight_smile:

1 Like

It looks like it was a permissions issue to the database.

The console was obviously providing me with root permissions, so it was working.

The API was failing, but no errors (as far as I could tell) were being thrown. It just returned an empty set.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.