链接Angular& Node.js:TypeError:error.json不是函数

时间:2018-01-11 13:43:50

标签: javascript json node.js angular

我正在研究一个连接node.js和Angular的课程。我正在学习本教程,但是我收到了一个错误,而且我不知道如何处理它。有人可以帮忙吗?

这是我得到的错误

  

TypeError:error.json不是函数       在CatchSubscriber.eval [作为选择器](message.service.ts?5cfd:25)       在CatchSubscriber.error(catchError.js?0867:104)       在MapSubscriber._next(map.js?c4af:82)       在MapSubscriber.Subscriber.next(Subscriber.js?215e:90)       在XMLHttpRequest.onLoad(http.js?7a71:1591)       在ZoneDelegate.invokeTask(zone.js?fad3:421)       at Object.onInvokeTask(core.js?223c:4744)       在ZoneDelegate.invokeTask(zone.js?fad3:420)       在Zone.runTask(zone.js?fad3:188)       在ZoneTask.invokeTask [作为调用](zone.js?fad3:495)       在invokeTask(zone.js?fad3:1536)       在XMLHttpRequest.globalZoneAwareCallback(zone.js?fad3:1562)

这些是文件。

消息输入组件在此输入新消息的内容:

import {Component} from "@angular/core";
import {MessageService} from "./message.service";
import {Message} from "./message.model";
import {NgForm} from "@angular/forms";

@Component({
    selector: 'app-message-input',
    templateUrl: './message-input.component.html'
})

export class MessageInputComponent {

    constructor(private messageService: MessageService){
    }

    onSubmit(form: NgForm) {
        const message = new Message(form.value.content, 'Tijl')
        this.messageService.addMessage(message)
            .subscribe(
                data => console.log(data),
                error => console.error(error)
            );
        form.resetForm()

    }
}

消息服务:这里我连接到Node.js后端

import {Message} from "./message.model";
import {Http, Response} from "@angular/http";
import {Injectable} from "@angular/core";
import 'rxjs/rx';
import {Observable} from "rxjs/Observable";


@Injectable()
export class MessageService {
    private messages: Message[] = [];

    constructor(private http: Http){}

    addMessage(message: Message) {
        this.messages.push(message)
        // turn your message into json format
        const body = JSON.stringify(message);
        // pass this body to the post request
        // we subscribe in the component itself, so not in this service. We need the data
        // in the component so that's where we'll subscribe to it
        return this.http.post('http://localhost:3000/message', body)
            // json method allows to extract the data (not headers and such)
        // and convert it to Javascript object
            .map((response: Response) => response.json())
            .catch((error: Response) => Observable.throw(error.json()));
    }
}

它连接的后端路由文件:

var express = require(' express'); var router = express.Router();

var Message = require(' ../ models / message');

router.post('/', function (req,res,next){
    var message = new Message({
        content: req.body.content
    });
    message.save(function(err, result) {
        if (err) {
            return res.status(500).json({
                title: 'An error occurred',
                error: err
            });
        }
        res.status(201).json({
            message: 'Saved message',
            // this object is what we'll receive in the front-end
            // and what we'll convert using the response.json() method
            obj: result
        })
    })
});

module.exports = router;

1 个答案:

答案 0 :(得分:1)

好的,

我发现了什么问题。代码很好,我只是忘了在进行更改后重启服务器。