decafjs / decaf

decaf core
MIT License
31 stars 7 forks source link

MongoDB driver and Objects in Collections #13

Open mm765 opened 8 years ago

mm765 commented 8 years ago

Last one for today...

I tried using the mongo-driver and did a simple find({}) (find all) and got the following error: \ EXCEPTION ** TypeError: Cannot find function entrySet in object en. at /opt/itk/libs/decaf-mongodb/lib/BSON.js:53 (deserialize) at /opt/itk/libs/decaf-mongodb/lib/BSON.js:75 (anonymous) at /usr/local/decaf/builtins/decaf.js:41 (anonymous) at /opt/itk/libs/decaf-mongodb/lib/BSON.js:74 (anonymous) at /usr/local/decaf/builtins/decaf.js:41 (anonymous) at /opt/itk/libs/decaf-mongodb/lib/BSON.js:53 (deserialize) at /opt/itk/libs/decaf-mongodb/lib/Cursor.js:217 (anonymous) at server.js:21 at /usr/local/decaf/builtins/rhino.js:117 (anonymous) at /usr/local/decaf/builtins/include.js:96 (includeFile) at /usr/local/decaf/builtins/include.js:107 (anonymous) at /usr/local/decaf/builtins/shell.js:72 (anonymous) at /usr/local/decaf/builtins/shell.js:18 at /usr/local/decaf/builtins/rhino.js:117 (anonymous) at /usr/local/decaf/builtins/include.js:96 (includeFile) at /usr/local/decaf/builtins/include.js:107 (anonymous) at /usr/local/decaf/builtins/all.js:160

The "en" in the error message lets me suspect that it has a problem with an object like Name['en'] = 'Choice' Name['de'] = 'Auswahl' in the collection - using a different collection that does not have an Object like that, works. Any idea ?

mm765 commented 8 years ago

Oh , this doesn't happen on the find() but when i try to access the data, either using cursor.next() or cursor.toArray()

mschwartz commented 8 years ago

Are you creating the DB with decaf?

I really can't say what's going on for sure without seeing the code. The data type of the element doesn't seem to be compatible with JavaScript.

On Tue, Jan 5, 2016 at 6:59 PM, mm765 notifications@github.com wrote:

Oh , this doesn't happen on the find() but when i try to access the data, either using cursor.next() or cursor.toArray()

— Reply to this email directly or view it on GitHub https://github.com/decafjs/decaf/issues/13#issuecomment-169205387.

mm765 commented 8 years ago

No, it is an existing database from a project (which im basically trying to re-create with decaf), my mongodb-related code has only a few lines:

eMongoDB = require('libs/decaf-mongodb').MongoDB eObjectID = require('libs/decaf-mongodb').ObjectId db = new eMongoDB('databasename') db.use('users') cu = db.users.find({}) console.log 'Count', cu.count() while(cu.hasNext()) data = cu.next() console.log 'Cursor:', data.Name

mm765 commented 8 years ago

Oh and the existing project also uses javascript (node.js, which i want to get rid of, because especially database access is a nightmare, codewise )

mm765 commented 8 years ago

Ok, using the debugger, it seems as if there is a problem when there is an array within an object in the document. The line from the database that creates the error (originating in Cursor.js: next() function, line ret=deserialize(ret); is: {"Languages"; ["en"], "LoginName": "b", ....} It seems as if it tries to handle the "en" as an object instead of as a string ?!

mschwartz commented 8 years ago

https://github.com/decafjs/decaf-mongodb/blob/master/lib/BSON.js#L47

You might step into deserialize() at that point and see where the logic is failing.

d(o) inside deserialize() right away.

On Tue, Jan 5, 2016 at 7:57 PM, mm765 notifications@github.com wrote:

Ok, using the debugger, it seems as if there is a problem when there is an array within an object in the document. The line from the database that creates the error (originating in Cursor.js: next() function, line ret=deserialize(ret); is: {"Languages"; ["en"], "LoginName": "b", ....} It seems as if it tries to handle the "en" as an object instead of as a string ?!

— Reply to this email directly or view it on GitHub https://github.com/decafjs/decaf/issues/13#issuecomment-169214361.

mm765 commented 8 years ago

Callstack: BSON.js: 53 (here it throws the exception) BSON.js:75 (here, value is "en") decaf.js:41

so it tries a deserialize on en which doesn't have an entrySet() function because it is a string.

BSON.js: 74 (here, elem is Languages=["en"], key is Languages and value ["en"] decaf.js: 41 again BSON.js: 53 again

mschwartz commented 8 years ago

Can you paste the output of d(o) into a message here?

On Tue, Jan 5, 2016 at 8:12 PM, mm765 notifications@github.com wrote:

Callstack: BSON.js: 53 (here it throws the exception) BSON.js:75 (here, value is "en") decaf.js:41

so it tries a deserialize on en which doesn't have an entrySet() function because it is a string.

BSON.js: 74 (here, elem is Languages=["en"], key is Languages and value ["en"] decaf.js: 41 again BSON.js: 53 again

— Reply to this email directly or view it on GitHub https://github.com/decafjs/decaf/issues/13#issuecomment-169215722.

mm765 commented 8 years ago

% d(o) (JavaObject) [getClass] [toCharArray] [wait] [codePointAt] [notifyAll] [replace] [regionMatches] [replaceFirst] [compareTo] [replaceAll] [getBytes] [substring] [notify] [empty] [split] [trim] [hashCode] [toUpperCase] [codePointBefore] [equalsIgnoreCase] [indexOf] [class] [compareToIgnoreCase] [codePointCount] [codePoints] [intern] [getChars] [toLowerCase] [contentEquals] [length] [isEmpty] [concat] [subSequence] [matches] [lastIndexOf] [contains] [bytes] [equals] [endsWith] [toString] [offsetByCodePoints] [chars] [charAt] [startsWith] [trimLeft] [trimRight] %

mschwartz commented 8 years ago

https://github.com/decafjs/decaf-mongodb/blob/master/lib/BSON.js#L47

At line 49, try adding this code:

if (!o.entrySet) { return String(o); }

On Tue, Jan 5, 2016 at 8:16 PM, mm765 notifications@github.com wrote:

% d(o) (JavaObject) [getClass] [toCharArray] [wait] [codePointAt] [notifyAll] [replace] [regionMatches] [replaceFirst] [compareTo] [replaceAll] [getBytes] [substring] [notify] [empty] [split] [trim] [hashCode] [toUpperCase] [codePointBefore] [equalsIgnoreCase] [indexOf] [class] [compareToIgnoreCase] [codePointCount] [codePoints] [intern] [getChars] [toLowerCase] [contentEquals] [length] [isEmpty] [concat] [subSequence] [matches] [lastIndexOf] [contains] [bytes] [equals] [endsWith] [toString] [offsetByCodePoints] [chars] [charAt] [startsWith] [trimLeft] [trimRight] %

— Reply to this email directly or view it on GitHub https://github.com/decafjs/decaf/issues/13#issuecomment-169216136.

mm765 commented 8 years ago

yes, that works

mm765 commented 8 years ago

thanks

mschwartz commented 8 years ago

That may not be the 100% correct solution, but it should hold you over until tomorrow when I can do the proper fix and push that code.

Maybe with this fix it won't work with array of numbers...

On Tue, Jan 5, 2016 at 8:23 PM, mm765 notifications@github.com wrote:

yes, that works

— Reply to this email directly or view it on GitHub https://github.com/decafjs/decaf/issues/13#issuecomment-169216641.

mm765 commented 8 years ago

Yeah, might be. One other thing - i just tried console.log(data.Languages) and got an error message: InternalError: Cannot convert org.mozilla.javascript.NativeArray@5ea434c8 to char

Works when i use console.log(data.Languages.toString()). Is that a "decaf thing" or a "rhino thing" ?

mm765 commented 8 years ago

Looks like rhino since decaf uses java.lang.system.out() .. Thanks for your help, gtg.

mschwartz commented 8 years ago

The idea is for modules to do the translations back and forth to/from Java and JavaScript.

The code I gave you casts that Java string to a JavaScript one.

On Tuesday, January 5, 2016, mm765 notifications@github.com wrote:

Looks like rhino since decaf uses java.lang.system.out() .. Thanks for your help, gtg.

— Reply to this email directly or view it on GitHub https://github.com/decafjs/decaf/issues/13#issuecomment-169217448.

mschwartz commented 8 years ago

This ticket really belongs in the decaf-mongoldb repo :)

I just pushed a better version of deserialize() that should fix the issue with arrays.