Angular Error: [object Object] resolvePromise
Angular Error: [object Object] resolvePromise
我正在使用 Angular 7 并使用 observable
调用简单服务
import { Injectable } from '@angular/core';
import { Http, Headers} from '@angular/http';
import { Observable } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class AuthService {
constructor(private http:Http) { }
registerUser(user):Observable<any>{
let headers = new Headers();
headers.append('Content-Type','application/json');
return this.http.post('http://localhost:8888/users/register', user, {headers:headers});
}
}
虽然编译代码没问题,但是当它命中寄存器 url 和组件时,它会给出错误
ERROR Error: "[object Object]"
resolvePromise http://localhost:4200/polyfills.js:3159:31
resolvePromise http://localhost:4200/polyfills.js:3116:17
scheduleResolveOrReject http://localhost:4200/polyfills.js:3218:17
invokeTask http://localhost:4200/polyfills.js:2766:17
onInvokeTask http://localhost:4200/vendor.js:72221:24
invokeTask http://localhost:4200/polyfills.js:2765:17
runTask http://localhost:4200/polyfills.js:2533:28
drainMicroTaskQueue http://localhost:4200/polyfills.js:2940:25
vendor.js:70671:5
defaultErrorLogger
http://localhost:4200/vendor.js:70671:5
./node_modules/@angular/core/fesm5/core.js/ErrorHandler.prototype.handleError
http://localhost:4200/vendor.js:70719:9
next
http://localhost:4200/vendor.js:72698:111
./node_modules/@angular/core/fesm5/core.js/EventEmitter.prototype.subscribe/schedulerFn<
http://localhost:4200/vendor.js:68245:36
./node_modules/rxjs/_esm5/internal/Subscriber.js/SafeSubscriber.prototype.__tryOrUnsub
http://localhost:4200/vendor.js:141501:13
./node_modules/rxjs/_esm5/internal/Subscriber.js/SafeSubscriber.prototype.next
http://localhost:4200/vendor.js:141439:17
./node_modules/rxjs/_esm5/internal/Subscriber.js/Subscriber.prototype._next
http://localhost:4200/vendor.js:141382:9
./node_modules/rxjs/_esm5/internal/Subscriber.js/Subscriber.prototype.next
http://localhost:4200/vendor.js:141359:13
./node_modules/rxjs/_esm5/internal/Subject.js/Subject.prototype.next
http://localhost:4200/vendor.js:141124:25
./node_modules/@angular/core/fesm5/core.js/EventEmitter.prototype.emit
http://localhost:4200/vendor.js:68229:54
onHandleError/<
http://localhost:4200/vendor.js:72252:57
./node_modules/zone.js/dist/zone.js/</ZoneDelegate.prototype.invoke
zone.js:388
./node_modules/zone.js/dist/zone.js/</Zone.prototype.run
zone.js:138
./node_modules/@angular/core/fesm5/core.js/NgZone.prototype.runOutsideAngular
http://localhost:4200/vendor.js:72189:16
onHandleError
http://localhost:4200/vendor.js:72252:13
./node_modules/zone.js/dist/zone.js/</ZoneDelegate.prototype.handleError
zone.js:392
./node_modules/zone.js/dist/zone.js/</Zone.prototype.runGuarded
zone.js:154
_loop_1
zone.js:677
./node_modules/zone.js/dist/zone.js/</</api.microtaskDrainDone
zone.js:686
drainMicroTaskQueu
e
zone.js:602
如果我从组件 //import {AuthService} from '../../services/auth.service';
中删除此服务 class,则代码可以正常运行。
尝试返回简单 return user
但仍然出现错误。
还尝试在没有观察到的情况下返回。
你能帮助我哪里做错了吗
Http
的使用自 Angular 5 起已弃用。因此它在 Angular 7.
中不可用
您应该使用 HttpClient
而不是 Http
。此外,默认添加 Content-Type
header。所以你不需要明确地添加它。
最后,http.post
已经 return 一个 Observable。所以你不需要将它包装在 of
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class AuthService {
constructor(private http: HttpClient) {}
registerUser(user): Observable <any> {
return this.http.post('http://localhost:8888/users/register', user);
}
}
您还必须将 HttpClientModule
添加到 AppModule
的 imports
数组中:
import { HttpClientModule } from '@angular/common/http';
@NgModule({
...,
imports: [..., HttpClientModule, ...]
...
})
然后在您的组件中:
constructor(private authService: AuthService) {}
...
const user = ...
this.authService.registerUser(user)
.subscribe(response => console.log(response));
...
我正在使用 Angular 7 并使用 observable
调用简单服务import { Injectable } from '@angular/core';
import { Http, Headers} from '@angular/http';
import { Observable } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class AuthService {
constructor(private http:Http) { }
registerUser(user):Observable<any>{
let headers = new Headers();
headers.append('Content-Type','application/json');
return this.http.post('http://localhost:8888/users/register', user, {headers:headers});
}
}
虽然编译代码没问题,但是当它命中寄存器 url 和组件时,它会给出错误
ERROR Error: "[object Object]"
resolvePromise http://localhost:4200/polyfills.js:3159:31
resolvePromise http://localhost:4200/polyfills.js:3116:17
scheduleResolveOrReject http://localhost:4200/polyfills.js:3218:17
invokeTask http://localhost:4200/polyfills.js:2766:17
onInvokeTask http://localhost:4200/vendor.js:72221:24
invokeTask http://localhost:4200/polyfills.js:2765:17
runTask http://localhost:4200/polyfills.js:2533:28
drainMicroTaskQueue http://localhost:4200/polyfills.js:2940:25
vendor.js:70671:5
defaultErrorLogger
http://localhost:4200/vendor.js:70671:5
./node_modules/@angular/core/fesm5/core.js/ErrorHandler.prototype.handleError
http://localhost:4200/vendor.js:70719:9
next
http://localhost:4200/vendor.js:72698:111
./node_modules/@angular/core/fesm5/core.js/EventEmitter.prototype.subscribe/schedulerFn<
http://localhost:4200/vendor.js:68245:36
./node_modules/rxjs/_esm5/internal/Subscriber.js/SafeSubscriber.prototype.__tryOrUnsub
http://localhost:4200/vendor.js:141501:13
./node_modules/rxjs/_esm5/internal/Subscriber.js/SafeSubscriber.prototype.next
http://localhost:4200/vendor.js:141439:17
./node_modules/rxjs/_esm5/internal/Subscriber.js/Subscriber.prototype._next
http://localhost:4200/vendor.js:141382:9
./node_modules/rxjs/_esm5/internal/Subscriber.js/Subscriber.prototype.next
http://localhost:4200/vendor.js:141359:13
./node_modules/rxjs/_esm5/internal/Subject.js/Subject.prototype.next
http://localhost:4200/vendor.js:141124:25
./node_modules/@angular/core/fesm5/core.js/EventEmitter.prototype.emit
http://localhost:4200/vendor.js:68229:54
onHandleError/<
http://localhost:4200/vendor.js:72252:57
./node_modules/zone.js/dist/zone.js/</ZoneDelegate.prototype.invoke
zone.js:388
./node_modules/zone.js/dist/zone.js/</Zone.prototype.run
zone.js:138
./node_modules/@angular/core/fesm5/core.js/NgZone.prototype.runOutsideAngular
http://localhost:4200/vendor.js:72189:16
onHandleError
http://localhost:4200/vendor.js:72252:13
./node_modules/zone.js/dist/zone.js/</ZoneDelegate.prototype.handleError
zone.js:392
./node_modules/zone.js/dist/zone.js/</Zone.prototype.runGuarded
zone.js:154
_loop_1
zone.js:677
./node_modules/zone.js/dist/zone.js/</</api.microtaskDrainDone
zone.js:686
drainMicroTaskQueu
e
zone.js:602
如果我从组件 //import {AuthService} from '../../services/auth.service';
中删除此服务 class,则代码可以正常运行。
尝试返回简单 return user
但仍然出现错误。
还尝试在没有观察到的情况下返回。
你能帮助我哪里做错了吗
Http
的使用自 Angular 5 起已弃用。因此它在 Angular 7.
您应该使用 HttpClient
而不是 Http
。此外,默认添加 Content-Type
header。所以你不需要明确地添加它。
最后,http.post
已经 return 一个 Observable。所以你不需要将它包装在 of
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class AuthService {
constructor(private http: HttpClient) {}
registerUser(user): Observable <any> {
return this.http.post('http://localhost:8888/users/register', user);
}
}
您还必须将 HttpClientModule
添加到 AppModule
的 imports
数组中:
import { HttpClientModule } from '@angular/common/http';
@NgModule({
...,
imports: [..., HttpClientModule, ...]
...
})
然后在您的组件中:
constructor(private authService: AuthService) {}
...
const user = ...
this.authService.registerUser(user)
.subscribe(response => console.log(response));
...