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

时间:2016-12-20 05:45:08

标签: javascript mongodb angular nativescript angular2-nativescript

我在使用nativescript app时遇到此异常。

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 

这就是我所拥有的:

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

@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

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);
};
}

点击登录按钮时出现此错误&#34;服务器错误&#34;。我该如何解决这个问题?我的网络和桌面应用程序与此代码完美配合。我正在使用mongoDb作为backend.am新的nativescript和angular2.Any帮助将非常有帮助和高度赞赏。

1 个答案:

答案 0 :(得分:0)

我不确定您使用哪个HTTP模块来调用此登录服务方法

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

根据我的经验,出于某些原因,您似乎无法使用本机Angular HTTP模块来执行HTTP请求。您必须使用Nativescript HTTP模块。所以像这样,

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

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


return getJSON("your url here")

查看官方文档:https://docs.nativescript.org/angular/code-samples/http.html