0

I'm having the following error:

error TS2339: Property 'includes' does not exist on type '{}'.

when trying to check if a username is taken or not. (It's perfectly working when I comment this error, then start with ng serve and then uncomment it but i cant launch my serve with the error, that's why I used this trick)

Service:

isUsernameAvailable(){
    const users = [];
    return new Promise(function(resolve, reject){
      firebase.database().ref('users').orderByKey().once('value').then(snapshot => {
        snapshot.forEach(childSnapshot => {
          if(childSnapshot.val().username){
            users.push(childSnapshot.val().username);
          }
        })
        resolve(users);

      })
    });


  }

Component:

async checkUsername(){
    const username=this.signupForm.get('username').value;
    const usernames =await this.authService.isUsernameAvailable();
    if(usernames.includes(username)){
      this.usernameAvailable=false;
    }
    else {
      this.usernameAvailable=true;
    }
  }

I saw some people having a similar error with type String[] and problem was in tsconfig with es2017, but I already have it:

"lib": [
      "es2017",
      "dom"
    ]

Where am I wrong ?

Emeric
  • 6,315
  • 2
  • 41
  • 54

1 Answers1

0

Firstly in this case the use of new Promise is not necessary, you can just return the promise. Secondly you need to should correctly type users as string[] that will help with the return type inference.

isUsernameAvailable() {
    return firebase.database().ref('users').orderByKey().once('value').then(snapshot => {
        const users:string[]  = [];
        snapshot.forEach(childSnapshot => {
            if (childSnapshot.val().username) {
                users.push(childSnapshot.val().username);
            }
        })
        return users;
    })
}
Titian Cernicova-Dragomir
  • 230,986
  • 31
  • 415
  • 357