Angular BodyParser不解析-但我觉得一切都很正常。如何解决“无法读取未定义的属性”`

Angular BodyParser不解析-但我觉得一切都很正常。如何解决“无法读取未定义的属性”`,angular,mongodb,express,Angular,Mongodb,Express,我一辈子都无法理解为什么到localhost:3000/api/v1/projects/add的post路由没有成功保存到数据库中。所有其他路由都可以工作,但发送到/添加的post路由返回500内部服务器错误。表示TypeError:无法读取未定义的属性“title” 控制台也会抛出和CORB错误: 跨源读取阻塞CORB阻塞跨源响应http://localhost:3000/api/v1/projects/add 使用MIME类型application/json。看见https://www.ch

我一辈子都无法理解为什么到localhost:3000/api/v1/projects/add的post路由没有成功保存到数据库中。所有其他路由都可以工作,但发送到/添加的post路由返回500内部服务器错误。表示TypeError:无法读取未定义的属性“title”

控制台也会抛出和CORB错误:

跨源读取阻塞CORB阻塞跨源响应http://localhost:3000/api/v1/projects/add 使用MIME类型application/json。看见https://www.chromestatus.com/feature/5629709824032768 更多细节

当I console.logprojectFormData时,它从Angular admin.service.ts saveProject方法发送的正确对象,但接收端显然有问题,因为无法解析req.body。而且它app.js bodyParser似乎在路由之前以正确的顺序加载

有人能发现错误吗

非常感谢您的帮助

快速应用程序

app.js

routes/projects.js

Angular客户端应用程序

app/admin/add project/add-project.component.ts

app/admin/admin.service.ts

回应

TypeError: Cannot read property 'title' of undefined

HTTP/1.1 500 Internal Server Error
X-DNS-Prefetch-Control: off
X-Frame-Options: SAMEORIGIN
Strict-Transport-Security: max-age=15552000; includeSubDomains
X-Download-Options: noopen
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: DELETE, PUT, GET, POST
Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization
Content-Security-Policy: default-src 'none'
Content-Type: text/html; charset=utf-8
Content-Length: 2242
Date: Fri, 16 Aug 2019 11:04:34 GMT
Connection: close

问题是您将api函数的参数按错误的顺序排列

router.post("/add", function(res, req, next) {
因此,您是从响应对象获取身体。但它没有。正确的顺序必须是

router.post("/add", function(req, res, next) {

当您记录请求正文时,您会得到什么?Hi Tien。我看到{title:这是我的标题,description:save goddanit}这意味着body解析器已经解析了dataSorry。我以前看错了console.log语句。如果我记录console.logREQ:BODY,req.BODY;我看到这是从服务器:REQ:BODY未定义中记录的。您可以使用postman来测试您的api吗?
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Project } from '../shared/project';

@Injectable({
  providedIn: 'root'
})
export class AdminService {
  saveProject(title: string, description: string) {
    const projectFormData: Project = { title, description };
    console.log(projectFormData);
    this.http
      .post<Project>(
        'http://localhost:3000/api/v1/projects/add',
        projectFormData
      )
      .subscribe(result => console.log(result));
  }

  constructor(private http: HttpClient) {}
}
POST /api/v1/projects/add HTTP/1.1
Host: localhost:3000
Content-Type: application/json
User-Agent: PostmanRuntime/7.15.2
Accept: */*
Cache-Control: no-cache
Postman-Token: cc6fd8d2-28c0-4ae7-9392-8602b9c8f401,e814d578-03ce-472d-be70-86f0e7b37373
Host: localhost:3000
Accept-Encoding: gzip, deflate
Content-Length: 62
Connection: keep-alive
cache-control: no-cache

{
    "title": "postman",
    "description": "this is the postman"
}
TypeError: Cannot read property 'title' of undefined

HTTP/1.1 500 Internal Server Error
X-DNS-Prefetch-Control: off
X-Frame-Options: SAMEORIGIN
Strict-Transport-Security: max-age=15552000; includeSubDomains
X-Download-Options: noopen
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: DELETE, PUT, GET, POST
Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization
Content-Security-Policy: default-src 'none'
Content-Type: text/html; charset=utf-8
Content-Length: 2242
Date: Fri, 16 Aug 2019 11:04:34 GMT
Connection: close
router.post("/add", function(res, req, next) {
router.post("/add", function(req, res, next) {