Async Custom Validator with API


#1

Hi !
I’m trying to making my async custom validator but I can’t manage to dot it. I’ve been trying to implement the same as here : https://forum.ionicframework.com/t/custom-validation-solved/87696/2
So in my code I use authService wich is a custom API to check if the email user is already in the database. I return a JSON with the following form :

{existingUser: true}

Here is my TypeScript code :

let emailChecker = (control: FormControl) => {
            return authService.checkEmail(control.value).then(data => data.existingUser ? {'bidon': true} : {}); console.log("false"));
        };

The problem is that data.existingUser is not known here because it comes from my API, so I’ve got the following error code

Property ‘existingUser’ does not exist on type ‘{}’.

The odd part is that I managed to make it work if ionic serve is running and that I first put data instead of data.existingUser and then putting back data.existingUser which does not generate any error .

My API method is the following :

exports.checkEmail = function(req, res, next){
    User.findOne({email: req.query.email}, function(err, existingUser){
        if(err){
            return next(err);
        }
        exist=false;
        if(existingUser){
            exist = true;
        }
        return res.status(200).json({
            existingUser: exist
        });
    });
}

And here is my Provider methode :

checkEmail(email){
        return new Promise((resolve, reject) => {
            let headers = new Headers();
            headers.append('Content-Type', 'application/json');            
            this.http.get('http://localhost:8080/api/auth/email', {headers: headers, search: {email: email}})
            .subscribe(res => {                
                let data = res.json();
                resolve(data);
                resolve(res.json());
                console.log(data);
            }, (err) => {
                reject(err);
            });            
        });
    }

So I’m probably doing something wrong but after more hours than I can count and the same amount of research on the web, I’m hoping someone will help me with this one.