I am putting together an admin function to create a user. Part of the creation of the user is to provide them access to certain databases.
In the code, I am sending an http post to my web api to create the user. Upon successful creation, I will then add the user-database bindings.
private createUserDatabases(createdUser: Users) {
console.log(JSON.stringify(createdUser));
if (this.isSuperAdmin && this.newUserDatabases.length > 0) {
var newUserDbs: UserDatabase[] = [];
for (var i = 0; i < this.newUserDatabases.length; i++) {
var newUserDatabase = new UserDatabase();
newUserDatabase.database_Id = this.newUserDatabases[i].id;
newUserDatabase.user_Id = createdUser.id;
newUserDbs.push(newUserDatabase);
//console.log("Getting ready to create");
//this.usersService.addUserToDatabase(newUserDatabase);
// .subscribe(
// data => console.log(JSON.stringify(data)),
// error => {
// this.alertService.error(error._body);
// console.log("userDb" + error._body);
// }
//);
}
console.log(newUserDbs);
return this.usersService.addUserToDatabase(newUserDbs);
//this.isOpened = false;
}
}
onSubmit() {
var createdUser: Users;
this.usersService.create(this.user)
.do(u => createdUser = u)
.flatMap(u => {
return this.createUserDatabases(u);
})
.subscribe(
data => {
console.log(JSON.stringify(data));
this.loadAllUsers();
this.isOpened = false;
},
error => {
this.alertService.error(error._body);
console.log(error._body);
}
)
}
The database bindings are being sent as an array of objects (string, string). I have tried this as individual calls per selected database, as well as a single call with all selected databases.
Here is the service
create(newUser: Users) {
return this.http.post(this.config.apiUrl + '/api/accounts/create', JSON.stringify(newUser), this.jwt())
.map((response: any) => response.json());
}
addUserToDatabase(newUserToDatabases: UserDatabase[]) {
console.log(JSON.stringify(newUserToDatabases));
return this.http.post(this.config.apiUrl + '/api/userdatabases/create', JSON.stringify(newUserToDatabases), this.jwt())
.map((response: any) => response.json());
}
Everything is completing as desired and neither of the requests are returning errors, however, I am receiving an undefined error and not making it to the data => portion of subscribe. I have found many articles and plunkrs that seem to imply this setup should work.