gpt4 book ai didi

javascript - 链接 Angular 和 Node.js : TypeError: error. json 不是函数

转载 作者:行者123 更新时间:2023-11-30 20:50:50 26 4
gpt4 key购买 nike

我正在学习一门连接 node.js 和 Angular 的类(class)。我正在按照教程进行操作,但出现错误,我不知道该怎么做。谁能帮忙?

这是我得到的错误

TypeError: error.json is not a function at CatchSubscriber.eval [as selector] (message.service.ts?5cfd:25) at CatchSubscriber.error (catchError.js?0867:104) at MapSubscriber._next (map.js?c4af:82) at MapSubscriber.Subscriber.next (Subscriber.js?215e:90) at XMLHttpRequest.onLoad (http.js?7a71:1591) at ZoneDelegate.invokeTask (zone.js?fad3:421) at Object.onInvokeTask (core.js?223c:4744) at ZoneDelegate.invokeTask (zone.js?fad3:420) at Zone.runTask (zone.js?fad3:188) at ZoneTask.invokeTask [as invoke] (zone.js?fad3:495) at invokeTask (zone.js?fad3:1536) at 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;

最佳答案

好的,伙计们,

我发现哪里出了问题。代码没问题,只是修改后忘了重启服务器。

关于javascript - 链接 Angular 和 Node.js : TypeError: error. json 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48208498/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com