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

using type.any() results in errors. #261

Closed
codedmart opened this issue Jun 12, 2015 · 5 comments
Closed

using type.any() results in errors. #261

codedmart opened this issue Jun 12, 2015 · 5 comments

Comments

@codedmart
Copy link

No matter how I try and use type.any() I get errors. Say I have something such as:

var thinky = require('thinky')();
var type = thinky.type;

var Model = Thinky.createModel('orders', {
    id: String,
    charge: type.any(),
});

data = {test: "test", data: "data"};

new Model(data).save()
.then...

I get an error of cannot read property test of undefined or if I remove test from the object then I get cannot read property data of undefined.

Thanks

@neumino
Copy link
Owner

neumino commented Jun 13, 2015

@codedmart -- I just tried your snippet, and it works fine for me.
Can you try to update thinky?

If you still get an error, can you provide the stacktrace? Thanks

@codedmart
Copy link
Author

Hmm.... I am still getting the error. I forgot to add {enforce_extra: 'remove'} to my example. I am using thinky 2.0.5

Possibly unhandled TypeError: Cannot read property 'test' of undefined
    at /Users/bmartin/Work/lumi/node_modules/thinky/lib/document.js:459:69
    at Object.loopKeys (/Users/bmartin/Work/lumi/node_modules/thinky/lib/util.js:203:16)
    at Document.__makeSavableCopy (/Users/bmartin/Work/lumi/node_modules/thinky/lib/document.js:456:10)
    at /Users/bmartin/Work/lumi/node_modules/thinky/lib/document.js:472:44
    at Object.loopKeys (/Users/bmartin/Work/lumi/node_modules/thinky/lib/util.js:203:16)
    at model.Document.__makeSavableCopy (/Users/bmartin/Work/lumi/node_modules/thinky/lib/document.js:456:10)
    at model.Document._makeSavableCopy (/Users/bmartin/Work/lumi/node_modules/thinky/lib/document.js:414:15)
    at /Users/bmartin/Work/lumi/node_modules/thinky/lib/document.js:626:21
    at tryCatch2 (/Users/bmartin/Work/lumi/node_modules/thinky/node_modules/bluebird/js/main/util.js:51:21)
    at Promise$_resolveFromResolver [as _resolveFromResolver] (/Users/bmartin/Work/lumi/node_modules/thinky/node_modules/bluebird/js/main/promise.js:585:13)
    at new Promise (/Users/bmartin/Work/lumi/node_modules/thinky/node_modules/bluebird/js/main/promise.js:82:37)
    at model.Document._saveHook (/Users/bmartin/Work/lumi/node_modules/thinky/lib/document.js:616:11)
    at _asyncHook.executeMain (/Users/bmartin/Work/lumi/node_modules/thinky/lib/util.js:159:17)
    at tryCatch (/Users/bmartin/Work/lumi/node_modules/thinky/lib/util.js:59:5)
    at _asyncHook.nextPre (/Users/bmartin/Work/lumi/node_modules/thinky/lib/util.js:172:5)
    at executeHooks (/Users/bmartin/Work/lumi/node_modules/thinky/lib/util.js:194:5)

I use thinky this way:

// thinky.js
var config = require('./config').config;
var thinky = require('thinky')(config.server.rethinkdb);

module.exports = thinky;
// model.js
var Thinky = require('./thinky');
var type = Thinky.type;

var Model = Thinky.createModel('orders', {
    id: String,
    charge: type.any(),
}, {enforce_extra: 'remove'});

data = {test: "test", data: "data"};

new Model(data).save()
.then...

@neumino
Copy link
Owner

neumino commented Jun 16, 2015

Hum, I'm a bit confused, I can't reproduce your error.

I tried this snippet and a few variants (changing the field name)

var thinky = require('thinky')();
var type = thinky.type;

var Model = thinky.createModel('orders', {
  id: String,
  test: type.any(),
}, {enforce_extra: 'remove'});

data = {test: "test", data: "data"};

var doc = new Model(data);
doc.save().then(function(result) {
  console.log(result);
}).error(function(error) {
  console.log(error);
});

The error seems to be that a field in the schema does not have the property _schema. While I'm not sure how to reproduce this, maybe the validation doesn't properly clean all the fields?

@codedmart
Copy link
Author

I am sorry @neumino. I have not written out what I am trying to do very well.

// thinky.js
var config = require('./config').config;
var thinky = require('thinky')(config.server.rethinkdb);

module.exports = thinky;
// model.js
var Thinky = require('./thinky');
var type = Thinky.type;

var Model = Thinky.createModel('orders', {
    id: String,
    charge: type.any(),
}, {enforce_extra: 'remove'});

data = {charge: {test: "test", data: "data"}};

new Model(data).save()
.then...

Really what I want to do is use {enforce_extra: 'remove}' but have certain attributes such as charge that are an object that I want to save the whole thing without having to type out/list all the attributes in the schema. Does that make sense? Thanks!

@neumino
Copy link
Owner

neumino commented Jun 18, 2015

@codedmart -- it should be fix in 2.0.6. Let me know if you run into other issues ;)

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

No branches or pull requests

2 participants