gpt4 book ai didi

node.js - Nodejs Api 参数传递和控制台记录参数

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

var express = require('express');
var app = express();
app.get('/products/productList/:id', (res, req) => {
console.log("product Id - " + req.params.id);
});

这段代码有什么问题?我该怎么做才能避免以下错误

TypeError: Cannot read property 'id' of undefined
at app.get (E:\Projects\Craiglist\trial\app.js:46:46)
at Layer.handle [as handle_request] (E:\Projects\Craiglist\trial\node_modules\express\lib\router\layer.js:95:5)
at next (E:\Projects\Craiglist\trial\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (E:\Projects\Craiglist\trial\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (E:\Projects\Craiglist\trial\node_modules\express\lib\router\layer.js:95:5)
at E:\Projects\Craiglist\trial\node_modules\express\lib\router\index.js:281:22
at param (E:\Projects\Craiglist\trial\node_modules\express\lib\router\index.js:354:14)
at param (E:\Projects\Craiglist\trial\node_modules\express\lib\router\index.js:365:14)
at Function.process_params (E:\Projects\Craiglist\trial\node_modules\express\lib\router\index.js:410:3)
at next (E:\Projects\Craiglist\trial\node_modules\express\lib\router\index.js:275:10)

最佳答案

(res, req)

您更改了函数参数的顺序:)

第一个参数是请求,第二个参数是响应。

试试这个:

app.get('/products/productList/:id', (req, res) => {
console.log("product Id - " + req.params.id);
});

关于node.js - Nodejs Api 参数传递和控制台记录参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49442522/

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