yaNastia
@yaNastia

Переписываю Http модуль на HttpClient как пофиксить возникшие ошибки?

ОШИБКИ:
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();
                });
        }
    }


Пример того что отправляю с бэкенда:

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.'
            });
        });
  • Вопрос задан
  • 667 просмотров
Пригласить эксперта
Ответы на вопрос 2
shai_hulud
@shai_hulud
А где все типы? Файлы .ts а указания типов нет. Он говорит что user это тип Object. А на Object нет полей "success", "message" итд. Нужно явно указать тип обьекта.
Ответ написан
Комментировать
@Dzhanik
Go
Создайте интерфейс с описанием типов user

interface user {
   success: boolean,
   message: string
   ...
}
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы