【问题标题】:undefined data using http.get() request to node.js from angular2?使用 http.get() 从 angular2 向 node.js 请求未定义的数据?
【发布时间】:2017-04-22 09:33:12
【问题描述】:

我正在尝试从 angular2 向 node.js 发出 http.get() 请求

在 CartService.ts..

@Injectable()
export class CartService {

   private CartUrl: string = '../cart'; // URL to Web API
   private headers: Headers = new Headers({'Content-Type': 'application/json'});

   constructor(private http: Http) {}

    public getCart(): Promise<Cart> {
        return this.http.get(this.CartUrl)
            .toPromise()
            .then(response => response.json().data as Cart)
            .catch(this.handleError);
    }


    public handleError(error: any): Promise<any> {
        console.error('An error occurred', error); 
        return Promise.reject(error.message || error);
    }

}

和 app.component.ts...

export class AppComponent implements OnInit {
     constructor (private cartService: CartService){}

     cart : Lecture[] = [];
      DBinfo : Cart;


    ngOnInit(): void {
        this.getCart();   
     }

     private getCart() : void{
         this.cartService.getCart()
              .then(DBinfo => this.DBinfo = DBinfo ,
                    ()=>console.log("The data is... "+this.DBinfo));

}

和来自 nodejs 的 index.js..

router.get('/cart', function (req, res,next) {

    var cart = {
        "email": "sAAAA@gmail.com",
        "item" : "bread"
    };

    res.json(cart);
});

当使用 this.getCart() 执行 ngOnInit 时,
console.log("数据是……"+this.DBinfo));
刚刚打印出“数据......未定义”

我怎样才能从 node.js 中获取数据..?
感谢您抽出宝贵时间阅读本文:)

【问题讨论】:

    标签: node.js angular


    【解决方案1】:

    来自then() 的第二个回调是错误回调!

    .then(data =&gt; ..., err =&gt; console.log(err));.

    所以在执行第二个回调时,您的请求期间似乎有任何错误!

    你从 node.js 返回的对象没有data 属性:

    .then(response =&gt; response.json() as Cart)

    我会使用Observables,这应该可以解决问题:

    public getCart(): Observable<Cart> {
       return this.http.get(this.CartUrl)
          .map(response => response.json() as Cart) // NO '.data' !!??
          .catch(this.handleError);
    }
    
    public handleError(error: any) {
       console.error('An error occurred', error); 
       return Observable.of(null);
    }
    
    private getCart(): void {
       this.cartService.getCart()
          .subscribe(DBinfo => this.DBinfo = DBinfo,
                     err => console.log(err),
                     () => console.log('done'));
    }
    

    【讨论】:

    • 我将()=&gt;console.log("The data is... "+this.DBinfo); 修复为err =&gt; console.log(err),然后稍后尝试使用console.log(DBinfo),但出现相同的结果...
    • 在第一个回调中添加一个额外的console.logdata =&gt; { console.log(data); this.data = data; }
    • 你的意思是修复它 getCart .then(DBinfo =&gt; {console.log(this.DBinfo); this.DBinfo = DBinfo} , err =&gt; console.log(err));?它是相同的“未定义”
    • 我不想修复某些东西,我想找到错误.. 那么哪个console.log 被解雇了?第一的?第二?两个都?没有? ..需要信息:)
    • 打印DBinfo或在分配后打印!
    猜你喜欢
    • 2019-06-25
    • 2017-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-04
    相关资源
    最近更新 更多