geddy / model

Datastore-agnostic ORM in JavaScript
265 stars 55 forks source link

belongsTo errors #87

Open mjp0 opened 11 years ago

mjp0 commented 11 years ago

I'm trying to get belongsTo work but I keep getting Uncaught TypeError: Cannot call method 'first' of undefined when calling Thing.getUser().

It's really simple structure. Model Thing has belongsTo('User'). I associate Thing to User via

User.first({id: id}, function(err, user) { 
    thing.setUser(user)
})

and then save thing again.

Now if I try thing.getUser(function...) I'll get the error above. If I look at the thing object I can see

userId: '69E7F802-E198-410A-BB1A-C417607529F8',
usersId: '69E7F802-E198-410A-BB1A-C417607529F8'

so obviously something got saved.

Any obvious reasons why this is happening? I'm using Riak.

mjp0 commented 11 years ago

Progress. I found out that I had User = model.register('Users', User); so I guess it was the plural that threw it off. I changed it to User = model.register('User', User); and now it seems to work.

mjp0 commented 11 years ago

There still seems to be a problem with saving. If I do thing.save(function(err, thing){ console.log(thing) }) I can see userId but if I fetch it again from the database, it haven't actually saved userId so getUser gives undefined. Is this Riak specific bug?

mde commented 11 years ago

I can't really understand from your description what the issue is. You've looked at the docs for associations? https://github.com/mde/model#associations I'll take a look and see if the common set of adapter tests are working on Riak or not. That would at least give you a starting point for code examples.

mde commented 11 years ago

All right, we've got the Riak tests working reliably again. Everything in the shared adapter tests are running in CI under Riak:

https://github.com/mde/model/blob/master/test/integration/adapters/shared.js

This should give you a lot of good examples of how the API works.

mjp0 commented 11 years ago

Ya, trust me, I've looked those over and over again ;)

NPM version doesn't seem to have Riak tests but I plugged adapter test from repo and ran jake test. I got this:

...
*** Running test/integration/adapters/riak/index.js ***
test create adapter
test save new, string UUID id, required nunmber is 0 (Riak)
test save new, string UUID id, required number is 1 (Riak)
test first via string id (Riak)
test datetime round-trip (Riak)
test first via object (Riak)
test updatePropeties does not affect datastore (Riak)
test save existing (Riak)
test save collection (Riak)
single-quote in string property (Riak)
test all, by string equality (Riak)
jake aborted.
Error: {"lineno":466,"message":"SyntaxError: syntax error","source":"()"}
    at IncomingMessage.<anonymous> (/Devs/POC/node_modules/model/node_modules/utilities/lib/request.js:124:15)
    at IncomingMessage.EventEmitter.emit (events.js:117:20)
    at _stream_readable.js:910:16
    at process._tickCallback (node.js:415:13)

I'm wondering that is this Riak bug, request bug or model bug? :)

edit I'm running Riak 1.4.1

mde commented 11 years ago

Right, we just got the tests working in master. Can you give master a shot and see if it fixes you up?

mjp0 commented 11 years ago

I removed the NPM version and replaced it with git clone - now tests are passing.

However, the weirdest thing - my code still doesn't work even it follows exactly what belongsTo test does. My code flows like this:

var thing = Thing.create({...})
if(thing.isValid()) {
   Users.first(uid, function(err, user) {
       thing.setUser(user)
       thing.save(function(err, thing1) {
           // point 1
           Thing.first(thing1.id, function(err, thing2) {
               // point 2
               thing2.getUser(function(err,user) {
                   assert.equal('foo@bar.com', user.email);
               })
           })
       })
   })
}

When we get to point 1 and do console.log(thing1) there's userId with correct id. When we get to point 2 and do console.log(thing2) userId can't be found and of course getUser() fails.

In the actual code after point 1 we jump to our test suite so that's why I'm doing another db query to verify it was properly saved.

mjp0 commented 11 years ago

I just noticed that same thing happens when I try to addThing to User model where there's hasMany('Things').

So there's something going on with saving all associations. They just don't seem to get saved for some reason.