Closed SudoPlz closed 9 years ago
Nevermind this was just a problem of Barrels, a library I used to populate my db automatically! All I did was populate the collection on my own and the problem was gone..! Sorry for the hustle!
Thanks for sharing the solution.
I'm building a sails.js api with MongoDb as my db. Those are my models (simplified just for the sake of this question):
models/Passport.js
simplified:models/User.js
simplified:_The problem_
In a sails.js service
login
function I first try to get the user by a username from theUser
collection.So far so good.
Then when the user is fetched (which is successfull), I grab his user.id and try to fetch his passport from the Passport collection.
The returned
passp
object is undefined. What... WHY?Additional info:
Running
> db.passport.find({owner:"theCorrectUserId"})
in my mongo shell DOES return the correct passport thus its not a matter of an incorrect id, and also, running :does contain all my passports. Thus its not a programming error either!
Aha moment:
What I've found over hours of banging my head on the wall is that _removing the association_ and thus making my models like so:
models/Passport.js
simplified:models/User.js
simplified:does solve the problem, and makes my
passp
object fetch the correct data..Am I using Waterline associations wrong? But then why does it work in mongo shell when the association exists?
Do I even need associations since I'm using 2 different queries in my service? What about performance?
Thank you..!