0

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.

2
  • what is the error? Commented Jul 21, 2017 at 15:21
  • It is just showing undefined Commented Jul 21, 2017 at 15:34

1 Answer 1

1

below your map code use

.catch((error)=> {
 return Observable throw(error)
 };
Sign up to request clarification or add additional context in comments.

2 Comments

may be you are expecting something in observable so Observable should be return see above code
You were onto something! I realized it was because I was mapping a JSON response despite the webapi only replying OK(); I removed the .map and that fixed it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.