【发布时间】:2021-01-30 01:07:30
【问题描述】:
大家!
我现在正在学习一些微服务原理,我对收到事件时的 query_service 错误有一些疑问
通常我的应用程序会创建一个帖子,并且在此帖子中用户可以放置一些 cmets,但我不知道发生了什么,我尝试了一切以在我的查询服务上解决这个问题,因为当用户制作时它会中断帖子中的评论
输出错误是:TypeError: Cannot read property 'cmets' of undefined
我的路由器代码:
import { Response, Request, Router } from 'express';
const routes = Router();
interface IPost {
id: string;
title: string;
comments: Comments[];
}
type Post = { [key: string]: IPost };
type Comments = { id: string, content: string };
interface IData {
id: string;
content: string;
postId: string;
}
let posts: Post = {}
routes.get('/posts', (req: Request, res: Response) => {
res.send(posts);
});
routes.post('/events', (req: Request, res: Response) => {
try {
const { type, data } = req.body;
if (type === 'PostCreated') {
const { id, title }: IPost = data;
posts[id] = { id, title, comments: [] };
}
if (type === 'CommentCreated') {
const { id, content, postId }: IData = data;
posts[postId].comments.push({id, content});
}
console.log(posts)
res.status(201).send({});
} catch (error) {
console.log(error)
}
});
export default routes;
其他一切正常。 任何人都可以在这个问题上帮助我吗? 如果您想查看整个程序,请访问该项目的 repo:Microservice API
【问题讨论】:
标签: javascript node.js typescript express microservices