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

500 when using array type #55

Closed
OscarGodson opened this issue May 9, 2013 · 15 comments
Closed

500 when using array type #55

OscarGodson opened this issue May 9, 2013 · 15 comments

Comments

@OscarGodson
Copy link
Member

In the model docs it says I can do:

this.defineProperties({
  foo: {type: 'array', required: true}
});

In my controller (create()) I have:

foo.save(function (err, data) {
  var fooId = data.id;
  bar.foos = [fooId];
  if (bar.isValid()) {
    bar.save(/* ... */);
  }
});

But it throws a 500

Error: 500 Internal Server Error

TypeError: Cannot read property 'validate' of undefined
at Object.utils.mixin.validateProperty (/usr/local/lib/node_modules/geddy/node_modules/model/lib/index.js:844:57)
at Object.utils.mixin.validateAndUpdateFromParams (/usr/local/lib/node_modules/geddy/node_modules/model/lib/index.js:804:24)
at Object.utils.mixin.createItem (/usr/local/lib/node_modules/geddy/node_modules/model/lib/index.js:717:17)
at Function.obj.create (/usr/local/lib/node_modules/geddy/node_modules/model/lib/index.js:444:31)
at create (/Users/oscar/Dropbox/projects/APP/app/controllers/users.js:26:33)
at callback [as last] (/usr/local/lib/node_modules/geddy/lib/controller/base_controller.js:349:22)
at async.AsyncBase.next (/usr/local/lib/node_modules/geddy/node_modules/utilities/lib/async.js:116:12)
at controller.BaseController._execFilters (/usr/local/lib/node_modules/geddy/lib/controller/base_controller.js:197:11)
at controller.BaseController._handleAction (/usr/local/lib/node_modules/geddy/lib/controller/base_controller.js:359:20)
at cb [as callback] (/usr/local/lib/node_modules/geddy/lib/app/index.js:279:36)

Simply changing it to a "string" type and changing bar.foos = [fooId]; to bar.foos = fooId; makes it work.

cc: @dadambickford

@techwraith
Copy link

I'm not sure why the Array type was removed, but I believe it will work if you change type: 'array' to type: 'object'.

@mde
Copy link
Contributor

mde commented May 9, 2013

Array datatype was kind of superfluous -- and it made the round-trip serialization story simpler just to say "we have all these specific scalar datatypes, and 'object' for anything else."

This was noted in the changelog -- sorry we missed removing it from the list of documented datatypes. I just removed it, and the site should pick up the changes sometime today. Let me know if you see any other references to it floating around. :)

@OscarGodson
Copy link
Member Author

Hmm, yeah I guess I could do {ids: [123, 345]}, but how easy is that to query with Mongo or Geddy's adapter? I'm basically using this as a join. A user can have multiple IDs and why i wanted it an array.

group.id = 123 // I get the group ID from some action in the UI, like deleting the group for example
user1.groups = {ids: [123, 456, 789]} // Wrapped in an object since I can't use an array
user2.groups = {ids: [xxx, yyy, 123, zzz]}

Now, whats the easiest way to query "all users part of 123"? Or am I doing this totally wrong?

@mde
Copy link
Contributor

mde commented May 9, 2013

No, you can save any JSONizable object in that datatype. Just declare it as datatype 'object', and save the array like normal.

Any reason not to use the Postgres adapter with eager-fetched associations and do an actual join? :) That's what that's for.

@techwraith
Copy link

Do this:

this.defineProperties({
  foo: {type: 'object', required: true}
});

// when setting
thing.foo = [id, otherId, etc];

Just call it an object (everything in JS is technically an object), but keep the value as an array.

@techwraith
Copy link

@mde Probably because SQL sucks.

@OscarGodson
Copy link
Member Author

Ah, I see, yeah I thought object meant a hash :)

@mde I probably will later, but thats why I'm using Geddy. I can build this out as a prototype and then swap out the DB without too much fuss! (well, I hope haha)

@mde
Copy link
Contributor

mde commented May 9, 2013

@techwraith You don't have to write any SQL, that's the whole point. :)

@techwraith
Copy link

@mde Well, if you only use Geddy to access the data, that's true, but if you ever have to edit data directly in the DB, that's not true. Though I suppose that's what geddy console is for.

@mde
Copy link
Contributor

mde commented May 9, 2013

NOSQL is great for things where you don't want any relationship between pieces of data. @OscarGodson is jumping through extra hoops to make non-relational data relational. If you actually want relationships between things, use a relational database. Then you can get the list of Users, the list of Groups, the list of Users per Group, and the list of Groups per Users. I keep thinking it's weird to see people a non-relational store, and then doing all these weird, hacky things to get relational behavior.

@OscarGodson
Copy link
Member Author

@mde For this it's just because I'm trying to get a prototype. I'm a noob at SQL and I've never interacted with Postgres directly. I will switch it for sure if it makes it past the prototype stage, but this is basically the only join I need to make for this stage. Geddy just allows me to plug-n-play™ DBs so I can put off the SQL learning curve for a bit ;)

@raphaeltm
Copy link

Hello.
I've got a similar, but slightly different problem.
I've got an array to be saved and it's not validating as an object. I'm not sure what other details to provide.
The exact validation error I receive is Tags: "tags" must be an object..

@mde
Copy link
Contributor

mde commented Aug 4, 2014

@raphaeltm Maybe we could start with a Gist of some of the problem code? Where are you setting the value for the tags prop?

@raphaeltm
Copy link

So, I'm new to Node and Geddy and I think I figured it out.

I have a Project model with a property tags: {type: 'object'}

In the "Create" action, after var project = geddy.model.Project.create(params);, I was converting a string of comma-separated tags from the params and assigning them to project.tags, like this:

project.tags = project.tagStringToArray(project.tags);

(a method I added to the model for this purpose. it returns the array of tags.)

The PHP frameworks I've used in the past allow this sort of assignment before validation.

With Geddy, it seems that if I use project.updateProperties() instead, everything works.

Sorry if this was a bit silly. I guess it will take me a little while to figure this all out.

Thanks for the prompt response, though.

@mde
Copy link
Contributor

mde commented Aug 4, 2014

No worries, glad you got it figured out. :)

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

4 participants