简体   繁体   English

未捕获(承诺):服务器错误

[英]Uncaught (in promise): Server error

I am getting this exception while using nativescript app. 使用nativescript应用程序时出现此异常。

EXCEPTION: Uncaught (in promise): Server error
JS: ORIGINAL STACKTRACE:
JS: Error: Uncaught (in promise): Server error
JS:     at resolvePromise (/data/data/com.yourdomain.appname/files/app/tns_modules/nativescript-angular/zone.js/dist/zone-nativescript.js:416:31)
JS:     at /data/data/com.yourdomain.appname/files/app/tns_modules/nativescript-angular/zone.js/dist/zone-nativescript.js:452:17
JS:     at ZoneDelegate.invokeTask (/data/data/com.yourdomain.appname/files/app/tns_modules/nativescript-angular/zone.js/dist/zone-nativescript.js:223:37)
JS:     at Object.onInvokeTask (/data/data/com.yourdomain.appname/files/app/tns_modules/@angular/core/bundles/core.umd.js:6197:41)
JS:     at ZoneDelegate.invokeTask (/data/data/com.yourdomain.appname/files/app/tns_modules/nativescript-angular/zone.js/dist/zone-nativescript.js:222:42)
JS:     at Zone.runTask (/data/data/com.yourdomain.appname/files/app/tns_modules/nativescript-angular/zone.js/dist/zone-nativescript.js:123:47)
JS:     at drainMicroTaskQueue (/data/data/com.yourdomain.appname/files/app/tns_modules/nativescript-angular/zone.js/dist/zone-nativescript.js:355:35)
JS: Unhandled Promise rejection: Server error ; Zone: angular ; Task: Promise.then ; Value: Server error 

This is what i have : 这就是我所拥有的:

login.component.tns.html login.component.tns.html

<StackLayout class="p-10">
<TextField [(ngModel)]="email" hint="email" autocorrect="false" autocapitalizationType="none"></TextField>
<TextField [(ngModel)]="password" hint="Password" secure="true"></TextField>
<Button text="signin" (tap)="login(user)"></Button>
<Button text="signup" (tap)="signup(user)"></Button>
</StackLayout>

login.component.ts login.component.ts

@BaseComponent({
moduleId: module.id,
selector: 'demo-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})

export class LoginComponent {
login() {
    this.user.password = this.password;
    this.user.username = this.email;
            this.loginService.signin(this.user)
        .subscribe(
        token => {
            this.loginCredentials.setUserId(token.userId);
            this.routerext.navigate(['/list'], {
                transition: {
                    duration: 1000,
                    name: 'slideTop',
                }
            });
        }
        )
};
}

login.service.ts login.service.ts

export class User {
constructor(
    public username: string,
    public password: string) { }
}

@Injectable()
export class LoginService {
signin(user: User) {
            return this.authModelApi.login({
        "email": user.username,
        "password": user.password
    })
        .map(token => token)
        .catch(this.handleError);
};
}

when clicking signin button i got this error "server Error". 单击登录按钮时,出现此错误“服务器错误”。 How should i resolve this issue? 我应该如何解决这个问题? My web and desktop app working perfectly with this code. 我的Web和桌面应用程序与此代码完美配合。 am using mongoDb as backend.am new to nativescript and angular2.Any help will really helpfull and highly appreciable. 我正在使用mongoDb作为nativescript和angular2的backend.am新手。任何帮助都将非常有用,而且非常可观。

I'm not sure which HTTP module you use to call this login service method 我不确定您使用哪个HTTP模块调用此登录服务方法

return this.authModelApi.login({
    "email": user.username,
    "password": user.password
})

From my experience, it seems for some reasons, you cannot use the native Angular HTTP module to perform HTTP requests. 根据我的经验,由于某些原因,您似乎无法使用本机Angular HTTP模块执行HTTP请求。 You have to use Nativescript HTTP module. 您必须使用Nativescript HTTP模块。 So something like this, 所以像这样

import { getJSON, request } from "http";
...

return request({
  url: "....",
  method: "POST"
  headers: [your headers],
  content: [your content]
})


return getJSON("your url here")

Check out the official documentation here: https://docs.nativescript.org/angular/code-samples/http.html 在此处查看官方文档: https : //docs.nativescript.org/angular/code-samples/http.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM