gpt4 book ai didi

javascript - 无法使用 mongodb 读取未定义的 MVC 模型 Web 服务的属性

转载 作者:太空宇宙 更新时间:2023-11-04 01:37:51 25 4
gpt4 key购买 nike

决定学习一些web服务开发,却遇到了一个问题。我似乎无法找到如何使用 ExpressJS Router() 将 POST 数据传递到我拥有的 Controller 文件的方法。当我读取从product.route.js 重新路由到product.controllerjs 的请求内容时,我得到

TypeError: Cannot read property 'name' of undefined

文件结构如下:

nodeTutorial/
app.js
controllers/
product.controller.js
models/
product.model.js
routes/
product.route.js

Node.js 依赖项

"dependencies": {
"body-parser": "^1.18.3",
"express": "^4.16.4",
"mongoose": "^5.4.1"
}

经过几个小时的调试后,我能够在不使用任何 Express 路由的情况下获取请求数据,只需通过执行以下操作即可在 app.js 中检索数据

app.post('/hello', function(req, res){
console.log("name: " + req.body.name + "; price: " + req.body.price);
res.json({requestBody: req.body})
});

这很好,但我想保持更有条理,而不是将所有内容都放入一个文件中。

app.js 代码

//app.js
const express = require('express');
const bodyParser = require('body-parser');
const product = require('./routes/product.route'); // Imports routes for the products
const app = express();


// Set up mongoose connection
//........

app.use(express.json());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use('/products', product);

//Method that worked for me here (explained above)

let port = 1234;
app.listen(port, () => {
console.log('Server is up and running on port number ' + port);
});

product.route.js

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

const product_controller =
require('../controllers/product.controller');

router.post('/create', product_controller.product_create);

module.exports = router;

product.controller.js

const Product = require('../models/product.model');

//Below is where i need to retrieve data from req
//As of now it prints 'req: undefined'
//And TypeError: Cannot read property 'name' of undefined
exports.product_create = function (req, res) {
console.log("req: "+ req.params.name);//doesnt work
//Also tried all of the below and all return undefied or an Empty Object
console.log("req.params.name: "+req.params.name);
console.log("req.params: "+ req.params);
console.log("req: "+ req.param("name"));
console.log("req.query.name: "+ req.query.name);
console.log("req.query: "+ req.query);
console.log("req.body.name: "+ req.body.name);
console.log("req.body: "+ req.body);
console.log("TEST");
let product = new Product(
{
name: req.body.name,
price: req.body.price
}
};

product.model.js

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

let ProductSchema = new Schema({
name: {type: String, required: true, max: 100},
price: {type: Number, required: true},
});

module.exports = mongoose.model('Product', ProductSchema);

当以下发布请求以名称和价格作为正文参数发送时,我需要将它们打印出来并分配给产品对象

POST /products/create HTTP/1.1
Host: localhost:1234
Content-Type: application/x-www-form-urlencoded
cache-control: no-cache
name=apple%2Cprice=15

提前抱歉,如果这是一个简单的解决方案,我对整个网络服务这件事很陌生。

最佳答案

所以你想在product.controller中处理与产品相关的路由。

在您的 app.js 中,app.use('/products',product.controller)

在产品 Controller 中,

const express=require('express')
const router=express.Router()

router.get('/create',(req,res)=>{//your code})

关于javascript - 无法使用 mongodb 读取未定义的 MVC 模型 Web 服务的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53939481/

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