Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteor mongo find() UnhandledPromiseRejectionWarning, maximum call stack size exceeded

Tags:

mongodb

meteor

I'm having a strange error while doing a mongodb find in my meteor app. The error being shown in the console is:

(node:20388) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2):
RangeError: Maximum call stack size exceeded

I've googled and found a couple of similar instances, but my circumstances are different and not tied to data size -- it's a very simple query and happens no matter what the query contents are.

On the client side, I do this:

Meteor.call( 'Things.search', searchString,(error,result) => {
      if(error){
        console.log(error);
      } else {
        console.log("Got result");
        ...

and it returns instantly from the server side with no error and result is undefined rather than being a cursor. The server side does this:

Meteor.methods({
  'Things.search'( searchString ) {
    check(searchString, String);
    process.on('unhandledRejection', r => console.log(r));
    try {
      let searchOptions = "$i";
      let result = Things.find({
        $or:[
          {typeOfThing:{ $regex: searchString, $options: searchOptions }},
          {name:{ $regex: searchString, $options: searchOptions }}]
        });
      return result;
    } catch (exception) {
      console.log( exception );
      throw new Meteor.Error('500', exception);
    }
  },
});

On a suggestion in another forum, I added the process.on() to reveal more about the underlying unhandled promise. After adding that, the console also showed this additional info:

RangeError: Maximum call stack size exceeded
I20180202-20:38:14.522(-5)?     at Object.keys.forEach.key (packages/ejson/ejson.js:594:27)
I20180202-20:38:14.522(-5)?     at Array.forEach (<anonymous>)
I20180202-20:38:14.525(-5)?     at Object.EJSON.clone.v [as clone] (packages/ejson/ejson.js:594:18)
I20180202-20:38:14.527(-5)?     at Object.keys.forEach.key (packages/ejson/ejson.js:595:22)
I20180202-20:38:14.528(-5)?     at Array.forEach (<anonymous>)
I20180202-20:38:14.530(-5)?     at Object.EJSON.clone.v [as clone] (packages/ejson/ejson.js:594:18)
I20180202-20:38:14.549(-5)?     at Object.keys.forEach.key (packages/ejson/ejson.js:595:22)
I20180202-20:38:14.551(-5)?     at Array.forEach (<anonymous>)
I20180202-20:38:14.553(-5)?     at Object.EJSON.clone.v [as clone] (packages/ejson/ejson.js:594:18)
I20180202-20:38:14.554(-5)?     at Object.keys.forEach.key (packages/ejson/ejson.js:595:22)

Using the chrome server debugger with meteor 1.6, I confirmed that there is an infinite loop in ejson.js.

Seems like a bug that's not mine -- I doubt find() should just go into an infinite loop. Does anyone have an idea about this?

like image 322
RealHandy Avatar asked Feb 06 '18 17:02

RealHandy


1 Answers

I found half the answer and a workaround, but now it just seems more likely to be a bug. If I just do the fetch() on the server side, rather than trying to return the cursor, it works fine! So, this works:

  let result = Things.find({
    $or:[
      {typeOfThing:{ $regex: searchString, $options: searchOptions }},
      {name:{ $regex: searchString, $options: searchOptions }}]
    }).fetch(); // <--- This fetch makes it all work fine!

Without the .fetch(), the result comes back immediately here as invalid (as shown in the debugger), as it all collapses in the infinite loop.

So, I'm past this problem by returning the fetch() results. It was my bug to not return the fetch() results, but it's a meteor issue that not doing so fails to report a useful error message and goes into an infinite loop.

like image 151
RealHandy Avatar answered Sep 25 '22 04:09

RealHandy