请求参数返回为未定义
Req Params Coming Back as Undefined
所以我有一个包含模拟数据的 JSON 文件。我正在尝试创建一个模拟 API 服务器。我有一条路线可以正常工作:
const express = require("express");
const router = express.Router();
const data = require('../data/customers.json')
router.get("/", (req, res) => {
try {
res.json({
customers: data
}).status(200);
} catch(error) {
console.error("ERROR: ",error);
return error;
}
})
我希望下一条路线是 localhost/customers/:id,但由于某些原因,当我将其测试为 localhost/customers/2 时,以下路线无法读取请求参数。但是,如果我为 2 分配一个测试变量并在 find 函数中使用它,它就可以工作。所以我将其缩小为 req.params 的问题。这是路线(当然在同一个文件中):
router.get("/:id", async (req, res) => {
const { customer_id } = req.params;
console.log('ID NUMBER', customer_id);
try {
const response = await data.find( customer => customer.id === customer_id);
console.log(response)
res.json(response).status(200);
} catch(error) {
console.error("ERROR: ",error);
return error;
}
})
如果有帮助的话,这里是我的 app.js:
的副本
'use strict';
const http = require('http');
const host = "127.0.0.1";
const port = 3000;
const cors = require('cors');
const morgan = require('morgan');
const express = require('express');
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(morgan('dev'));
app.use(cors());
app.use(express.static('public'));
const server = http.createServer(app);
server.listen(port, host, () => {
console.log('Server has started.')
});
const rootController = require('./routes/index');
const customersController = require('./routes/customers');
app.use('/', rootController);
app.use('/customers', customersController);
问题出在您的 router.get
上。您将参数称为 customer_id
。您需要在 url router.get("/:id", async (req, res)...
中以与参数相同的名称引用它,因此您的 req.param
是 { id: '' }
.
将const { customer_id } = req.params;
替换为const { id } = req.params;
所以我有一个包含模拟数据的 JSON 文件。我正在尝试创建一个模拟 API 服务器。我有一条路线可以正常工作:
const express = require("express");
const router = express.Router();
const data = require('../data/customers.json')
router.get("/", (req, res) => {
try {
res.json({
customers: data
}).status(200);
} catch(error) {
console.error("ERROR: ",error);
return error;
}
})
我希望下一条路线是 localhost/customers/:id,但由于某些原因,当我将其测试为 localhost/customers/2 时,以下路线无法读取请求参数。但是,如果我为 2 分配一个测试变量并在 find 函数中使用它,它就可以工作。所以我将其缩小为 req.params 的问题。这是路线(当然在同一个文件中):
router.get("/:id", async (req, res) => {
const { customer_id } = req.params;
console.log('ID NUMBER', customer_id);
try {
const response = await data.find( customer => customer.id === customer_id);
console.log(response)
res.json(response).status(200);
} catch(error) {
console.error("ERROR: ",error);
return error;
}
})
如果有帮助的话,这里是我的 app.js:
的副本'use strict';
const http = require('http');
const host = "127.0.0.1";
const port = 3000;
const cors = require('cors');
const morgan = require('morgan');
const express = require('express');
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(morgan('dev'));
app.use(cors());
app.use(express.static('public'));
const server = http.createServer(app);
server.listen(port, host, () => {
console.log('Server has started.')
});
const rootController = require('./routes/index');
const customersController = require('./routes/customers');
app.use('/', rootController);
app.use('/customers', customersController);
问题出在您的 router.get
上。您将参数称为 customer_id
。您需要在 url router.get("/:id", async (req, res)...
中以与参数相同的名称引用它,因此您的 req.param
是 { id: '' }
.
将const { customer_id } = req.params;
替换为const { id } = req.params;