neumino / thinky

JavaScript ORM for RethinkDB
http://justonepixel.com/thinky/
Other
1.12k stars 128 forks source link

Pluck, etc. not working - ReqlDriverError #219

Closed arunr closed 9 years ago

arunr commented 9 years ago

My controller code looks like this:

Outlet.filter(req.query.q).pluck('name').run().then(function(result) {
    res.send(JSON.stringify(result));
 }).error(handleError(res));

When I run this by hitting the route, I get the error:

Possibly unhandled ReqlDriverError: `toJSON` takes 0 argument, 1 provided after:

Any ideas where I am going wrong? Thanks!

neumino commented 9 years ago

You're probably calling JSON.stringify on a query and not the result of a query. A query will implement to JSON as a ReQL term.

lorenz commented 9 years ago

I could reproduce it, it occurs specifically when you use r.now() as default in a thinky model. It woks until you have an object that misses a date and then it defaults it to something like date: { [Function] _query: [Object], _r: [Object], _error: null } }, which can't get stringified.

neumino commented 9 years ago

You can't serialize r.now() to JSON.

If you want to serialize documents with a default field that default to "now", use function() { return new Date() } as the default value instead of r.now().

neumino commented 9 years ago

Closing as this is not a bug. If you have more questions, feel free to ask.

avimar commented 9 years ago

I seemed to be hitting this and had no clue until I googled and found this. I was returning the prepared data I inserted into rethink (since I didn't ask for it back) and had used r.epochtime in many places (not r.now()). Not sure if this happened from logging or returning the data as res.json in restify. I need to do more testing around this...

I'm using rethinkdbdash directly, not thinky. Is there a way to have it properly .toString/toJSON as a javascript date?

neumino commented 9 years ago

Why would you serialize a ReQL term? What's your use case?

avimar commented 9 years ago

My use case: partly for debugging, partly for testing - I'm doing several transformations on FreeSWITCH call records to make them 'natively' query-able in rethinkdb.

I narrowed down to: restify's res.json(200,data) of the direct query sent to rethinkdb is a problem (but console.log and winston logging is fine). I was able to replace r.epochTime(number/1000/1000); with new Date(number/1000); and all seems to be working (except slightly lower resolution) without restify complaining and failing.

neumino commented 9 years ago

It still doesn't make sense to me, why would you send back a ReQL term back to the browser? The ReQL term is supposed to be sent only to a RethinkDB server.

lilhamad commented 3 years ago

using something liker.now and are r.uuid() throws error when dealing with Json atimes use the following instead