无法将数据从后端(nodejs)发送到前端(angular2)

时间:2017-08-15 09:34:47

标签: node.js angular express elasticsearch

我有一些数据,我通过nodejs从elasticsearch查询,我想将它们发送到前端。当我尝试这样做时,它会向我显示如下错误。

ERROR Error: Uncaught (in promise): Response with status: 0  for URL: null
at resolvePromise (zone.js:783)
at zone.js:709
at rejected (messages-component.js:14)
at ZoneDelegate.invoke (zone.js:391)
at Object.onInvoke (ng_zone.ts:296)
at ZoneDelegate.invoke (zone.js:390)
at Zone.run (zone.js:141)
at zone.js:831
at ZoneDelegate.invokeTask (zone.js:424)
at Object.onInvokeTask (ng_zone.ts:288)

后端看起来像这样,

var name = ".class";

rama();

function rama() {
'use strict';

const elasticsearch = require('elasticsearch');
const esClient = new elasticsearch.Client({
  host: '127.0.0.1:9200',
  log: 'error'
});

const search = function search(index, body) {
  return esClient.search({index: index, body: body});
};

const test = function test() {
  let body = {
    size: 20,
    from: 0,
    query: {
      match: {
        JarFileName: {
          query: 'CambioClient.jar'
        }
      }
    }
  };
  search('clsdex', body)
  .then(results => {
    results.hits.hits.forEach((hit, index) => name="Test");
  })
  .catch(console.error);
};

test();
};

var express = require('express');
var app = express();

var messages = [{text: name, owner: 'Tim'},{text: 'other message', owner:'Jane'}];

app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, 
Content-Type, Accept");
next();
})

app.get('/messages', (req, res) => {
res.json(messages);
})

app.listen(11500);

这是我收到结果的前端(message-component.ts)中的部分,

import { Component } from '@angular/core';
import { WebService } from './web.service';

@Component({
selector: 'messages',
template: `
<div *ngFor="let message of messages">
    <md-card style="margin:8px">
        <md-card-title>{{message.owner}}</md-card-title>
        <md-card-content>{{message.text}}</md-card-content>
    </md-card>
</div>
`
})
export class MessagesComponent {

messages = [];

constructor(private webService: WebService) {}

async ngOnInit() {
    var response = await this.webService.getMessages();
    this.messages = response.json();
}
}

我在哪里弄错了?请帮我搞清楚。

0 个答案:

没有答案