Uploaded image for project: 'Node.js Driver'
  1. Node.js Driver
  2. NODE-528

Ignore undefined fields in Collection.find()

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 2.0.40
    • Affects Version/s: None
    • Component/s: None
    • Labels:

      https://github.com/mongodb/node-mongodb-native/pull/1284

      I noticed when calling the `find(selector, fields)` method on a collection with fields set to `undefined`, I get a `TypeError: Object.keys called on non-object`. When checking out the problem, I found that the `collection.js` module checks if `argument.length >= 2` which evaluates to `true` even if `argument[1]` is not explicitly set. I.e. the code is trying to use the `fields` argument as if it's set, throwing the error eventually.

      Fixed it by comparing `fields !== undefined` instead of using the argument count.

      In our application, we basically do something like this:
      ```js
      // fields is optional
      function getRecords(arg1, arg2, fields) {
      // validate arg1, arg2 etc...

      // build the query with arg1, arg2
      var query = {};

      return collection.find(query, fields);
      }
      ```

      I changed `fields` to `fields || {}` as a quick work-around for now, but I'm pretty sure `undefined` should be just ignored in the module.

            Assignee:
            christkv Christian Amor Kvalheim
            Reporter:
            christkv Christian Amor Kvalheim
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: