derbyjs / racer

Realtime model synchronization engine for Node.js
1.19k stars 118 forks source link

model.fetch() returns null in middleware #39

Closed lefnire closed 12 years ago

lefnire commented 12 years ago

I'm almost done with a Derby example in EveryAuth. The problem is in findOrCreateUser(). It uses model.fetch(query, ..) to determine if the user exists, and model.set(...) to create new user otherwise. However, that model.fetch keeps returning {err:null, user:null}, yet the same query run outside of EveryAuth's middleware (aka, on the client) works properly and returns a user.

Here's the code where it's flopping, and check out server/index.js for which locations call these functions.

lefnire commented 12 years ago

Actually, after playing a bit - maybe model.fetch() (and thereby model.get()) can't be run on the server at all (server/*.js, it's fine on app/*.js)? I can run model.set(..) just fine, but model.fetch(.., function(err,x){console.log(x);}) always returns null when I run it on the server. I got EveryAuth working when I do some jury-rigging such that a check is run on the client and redirects on success - see this commit. This works fine, but not in accordance with EveryAuth's standard setup using findOrCreateUser + findUserById on the server.

lackac commented 12 years ago

Just a few thoughts without looking at your code (I'll do that later if I find the time). Can't you use model.setNull for making sure the user exists? I don't think this will solve your issue though.

Also model.fetch did work for me on the server side. Since bible chapters are not going to change this is what I use in my bible reading application. Here's the code snippet that works for me:

      model.fetch "chapters.#{ref.id}", (err, chapter) ->
        model.ref '_chapter', chapter
        page.render()
lefnire commented 12 years ago

I should say it runs fine on the server in app/index.js (your routes that Derby sets up), but not on server/index.js - namely in custom middleware functions, which is where EveryAuth would require hooking into. Laszlo, would love to take a peak at your Bible app code sometime.

lackac commented 12 years ago

I know, I'm thinking about the best way for making it open source. For one, the user interface is Hungarian and I don't plan on internationalizing it for now. Also, you would need a Bible translation in the database and the only one I have which is in the public domain is also Hungarian. There are some other considerations about license, etc.

lefnire commented 12 years ago

It was my own code. Because I was using query instead of path, it was returning multiple results and I needed to grab results[0]. It turns out console.log(users) was showing me undefined, rather than a model.at object. When I did step-through debugging via Webstorm (highly recommended, just discovered it) I was able to see that in fact results were being returned, but because I was calling .get('id') on a returned array, I was getting undefined.