Reputation: 321
I've recently started to code in angular as the frontend code and was planning to use loopback (www.loopback.io) as my backend (with a mongodb for my storage).
Now, as a basic idea I'm just creating a login form and register form and will go on from there. Yet, I can't seem to get my models correct, especially the relations between them.
I'd like a user model for the basic username/password stuff, and a person model where I can store more info like firstname, lastname, and more.
I'm currently using the "default" User model that you get out of the box and created a person model with the needed properties.
What I can't get correct though, is the link between the two (a one to one relation, so the HasOne relation).
These are my models:
user.json
{
"name": "user",
"base": "User",
"idInjection": true,
"properties": {},
"validations": [],
"relations": {
"person": {
"type": "hasOne",
"model": "Person",
"foreignKey": "id"
}
},
"acls": [],
"methods": []
}
person.json
{
"name": "Person",
"plural": "Person",
"base": "PersistedModel",
"idInjection": true,
"properties": {
"FirstName": {
"type": "string",
"required": true
},
"LastName": {
"type": "string",
"required": true
}
},
"validations": [],
"relations": {},
"acls": [],
"methods": []
}
my register function
(I'm using the automaticaly generated lb-services file you can extract with the angular SDK provided by loopback, that's why you just see stuff like "User.create" in stead of a "real" api call)
function register(email, password, firstname, lastname) {
var usr = User.create({
email: email,
password: password
}).$promise;
var prs = usr.person.create({
firstname: firstname,
lastname: lastname
});
return usr;
}
There's bound to be a bunch of stuff wrong here, I just don't know what. The user gets created in the database, yet I can't seem to instantly also create a "person" object and link that to the newly created user.
Any help is appreciated, cause I can't seem to find that many resources online that use angular and loopback with the generated lb-services file...
Upvotes: 2
Views: 944
Reputation: 2290
Seems that you reference undefined property person
of usr
instance.
Create person using User resource, in success callback:
function register(email, password, firstname, lastname) {
var usr = User.create({
email: email,
password: password
}, function() {
User.person.create({ userId: usr.id }, {
firstname: firstname,
lastname: lastname
}, function(person) {
console.log('Persone created:', persone)
})
});
return usr;
}
Upvotes: 1