A
A
asd dsa2018-11-28 17:21:41
JavaScript
asd dsa, 2018-11-28 17:21:41

I rewrite the Http module on HttpClient, how to fix the errors that have arisen?

ERRORS:
ERROR in src/app/common/auth.service.ts(44,30): error TS2339: Property 'success' does not exist on type 'Object'.
src/app/common/auth.service.ts(45,56): error TS2339: Property 'message' does not exist on type 'Object'.
src/app/common/auth.service.ts(47,45): error TS2339: Property 'message' does not exist on type 'Object'.
src/app/common/auth.service.ts(48,46): error TS2339: Property 'token' does not exist on type 'Object'.
src/app/login/login.component.ts(33,30): error TS2339: Property 'success' does not exist on type 'Object | any[]'.
Property 'success' does not exist on type 'Object'.
src/app/login/login.component.ts(34,50): error TS2339: Property ' message' does not exist on type 'Object | any[]'.
Property 'message' does not exist on type 'Object'.

auth.service :

login(oUser) {
        return this.http.post('http://localhost:1978/api/login', JSON.stringify(oUser), httpOptions).pipe(
                tap(user => {
                    if (user.success) {
                        this.currentUser = <IUser>user.message;
                        let userObj: any = {};
                        userObj.user = user.message;
                        userObj.token = user.token;
                        localStorage.setItem('currentUser', JSON.stringify(userObj));
                    }
                }),
            catchError(this.handleError('login', []))
    );
}

login component.ts
loginUser(): void {
        if (this.loginForm.dirty && this.loginForm.valid) {
            this.authService.login(this.loginForm.value)
                .subscribe(data => {
                    if (data.success === false) {
                        this.messages.error(data.message);
                    } else {
                        this.messages.success('Login successful.');
                        this.router.navigate(['report']);
                    }
                    this.loginForm.reset();
                });
        }
    }

An example of what I send from the backend:
oUser.save(function(err) {
            if(err){ res.status(400).json({ success: false, message:`Error processing request ${err}`}); }

            res.status(201).json({
                success: true,
                message: 'User created successfully, please login to access your account.'
            });
        });

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
shai_hulud, 2018-11-28
@shai_hulud

Where are all types? .ts files and no type specification. It says that user is of type Object . And on Object there are no "success", "message" fields, etc. You must explicitly specify the type of the object.

J
Johny, 2018-12-11
Marx

Create an interface with a description of user types

interface user {
   success: boolean,
   message: string
   ...
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question