Closed arunr closed 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.
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.
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()
.
Closing as this is not a bug. If you have more questions, feel free to ask.
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?
Why would you serialize a ReQL term? What's your use case?
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.
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.
using something liker.now
and are r.uuid()
throws error when dealing with Json atimes use the following instead
function() { return new Date() }
instead of r.now
new Guid().StringGuid
instead of r.uuid()
Like below:
import { Guid } from 'js-guid';
var Order = thinky.createModel("Order", {
id: type.string().default(new Guid().StringGuid),
updatedAt: type.date().default(function() { return new Date() })
});
install js-guid with npm install js-guid
My controller code looks like this:
When I run this by hitting the route, I get the error:
Any ideas where I am going wrong? Thanks!