Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Work around for property 'Symbol(Symbol.toPrimitive)' in Proxy handler #332

Merged
merged 4 commits into from
Sep 10, 2017

Conversation

ChuanyuWang
Copy link
Contributor

When I am trying to migrate to higher version nodeJS, e.g. 6.9.1. I found below error coming from Proxy handler, which is caused by Symbol property. So I add another workaround to skip Symbol property during Proxy's get handler

TypeError: '[object Object]' returned for property 'Symbol(Symbol.toPrimitive)' of object '[object Object]' is not a function
    at exports.format (util.js:108:18)
    at Console.log (console.js:43:37)
    at Database.<anonymous> (C:\git\test\app2.js:33:13)
    at emitNone (events.js:86:13)
    at Database.emit (events.js:185:7)
    at C:\git\test\node_modules\mongojs\lib\database.js:35:14
    at connectCallback (C:\git\test\node_modules\mongodb\lib\mongo_client.js:426:5)
    at C:\git\test\node_modules\mongodb\lib\mongo_client.js:364:13
    at _combinedTickCallback (internal/process/next_tick.js:67:7)
    at process._tickCallback (internal/process/next_tick.js:98:9)

Fix parsing database name from native driver to pass all test

Chuanyu Wang added 3 commits February 28, 2017 11:34
…he same way.

Update the detection in database.js to support handling newer native objects.

copy zxaos's fix to pass the tests
@ChuanyuWang
Copy link
Contributor Author

@saintedlama I am fan of mongojs, and it's the best nodejs API wrapper I used. As there are five open pull request to mongoja, may you help merge them and publish a new version?

@saintedlama
Copy link
Collaborator

I'm using node 8 and did not have any issue with Symbols at all. Could you explain to me why this PR is important and what it fixes?

@ChuanyuWang
Copy link
Contributor Author

ChuanyuWang commented Sep 4, 2017

Glad to see you reply. Here is the sample code to reproduce this issue.

var mongojs = require('mongojs');
var db = mongojs(uriString);
console.log("" + db.toString()); // Good
console.log("" + db); // TypeError: '[object Object]' returned for property 'Symbol(Symbol.toPrimitive)' of object '[object Object]' is not a function

Testing on Window 10 64bit:

  • node@6.9.1
  • mongodb@3.2.5

The Symbol.toPrimitive is used to convert db object to primitive value, but it's treated as a collection name in file index.js line 15

var Database = require('./lib/database')
var mongodb = require('mongodb')

module.exports = function (connString, cols, options) {
  var db = new Database(connString, cols, options)
  if (typeof Proxy !== 'undefined') {
    var handler = {
      get: function (obj, prop) {
        // Work around for event emitters to work together with harmony proxy
        if (prop === 'on' || prop === 'emit') {
          return db[prop].bind(db)
        }

        if (db[prop]) return db[prop]
        db[prop] = db.collection(prop) // <== prop is actually a system level Symbol
        return db[prop]
      }
    }

    return Proxy.create === undefined ? new Proxy({}, handler) : Proxy.create(handler)
  }

  return db
}

ChuanyuWang pushed a commit to ChuanyuWang/test that referenced this pull request Sep 4, 2017
@saintedlama saintedlama merged commit 5dcf527 into mongo-js:master Sep 10, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants