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

Rea*_*ndy 5 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
Run Code Online (Sandbox Code Playgroud)

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");
        ...
Run Code Online (Sandbox Code Playgroud)

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);
    }
  },
});
Run Code Online (Sandbox Code Playgroud)

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)
Run Code Online (Sandbox Code Playgroud)

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

似乎不是我的错误——我怀疑 find() 应该进入无限循环。有没有人有这个想法?

Rea*_*ndy 6

我找到了一半的答案和解决方法,但现在它似乎更有可能是一个错误。如果我只是在服务器端执行 fetch(),而不是尝试返回游标,它工作正常!所以,这有效:

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

如果没有.fetch(),结果会立即返回无效(如调试器中所示),因为它在无限循环中全部崩溃。

所以,我通过返回 fetch() 结果解决了这个问题。不返回 fetch() 结果是我的错误,但这是一个流星问题,不这样做无法报告有用的错误消息并进入无限循环。