【发布时间】:2025-12-29 18:30:06
【问题描述】:
我被这个错误困住了 - Can't set Headers of Undefined。
代码参考
单独定义标题:
var headers = function (req,res,next){
res.setHeader("Access-Control-Allow-Origin", "[*]");
res.setHeader("Access-Control-Allow-Headers","Origin, X-Requested-With, Content-Type, Accept");
res.setHeader("Access-Control-Allow-Methods","GET, POST, PATCH, DELETE, OPTIONS");
console.log("Inside headers")
}
定义了我想运行的第一个函数:
var middleware1 = function (req, res, next) {
headers();
console.log("Before Get method middleware")
console.log("middleware1");
collection.find({}, {
name: 'Task 1'
}).toArray((error, result) => {
if (error) {
console.log("It is the find -> /tasks method error");
return res.status(500).send(error);
}
next();
})
}
定义第二个函数:
var middleware2 = function (req, res, next) {
headers();
console.log("middleware2");
collection.find({}, {name: 'Task 4'}).toArray((error, result) => {
if (error) {
console.log("It is the find -> /tasks method error");
return res.status(500).send(error);
}
})
next();
}
定义了 validateParams:
Defined the Final value to be run:
const fn1 = (req, res, next) => {
middleware1();
console.log("1");
next();
};
const fn2 = (req, res, next) =>{
middleware2();
console.log("2");
next();
};
const fn3 = (req, res, result) => {
collection.find({}).toArray((error, result) => {
if (error) {
console.log("It is the find -> /tasks method error");
return res.status(500).send(error);
}
res.send(result);
console.log("result");
});
};
**Final Call**
app.get("/2498", function fn1(req, res, next) {
headers(req, res, next);
//In Going the right way --> Task 1 to Task 4
middleware1(req, res, next);
console.log("1");
// next();
},
function fn2 (req,res,next) {
headers(req, res, next);
middleware2(req, res, next);
console.log("2");
// next();
} ,function fn3 (req,res,next) {
headers(req, res, next);
collection.update(
{ 'taskId': 1, 'parent': 'yes!' , 'projectId' : 1,
'content': { 'name': 'Task 1', 'predecessors':[], 'successor': [2] } },
{ 'taskId': 4, 'parent': 'na' , 'projectId' : 2,
'content': { 'name': 'Task 6' , 'predecessors':[], 'successor': [3] } }
).toArray((error, result) => {
if(error) {
console.log("It is the find -> /tasks method error");
return res.status(500).send(error);
}
console.log("Inside getToBeAssumed --> Task 4
.....validateParams.....");
console.log("The end = ", result);
})
})
```
详细错误:
TypeError:无法读取未定义的属性“setHeader” 在标题处 (D:\mongodb_test_app\app-module-1.js:231:7) 在 fn1 (D:\mongodb_test_app\app-module-1.js:303:7) 在 Layer.handle [as handle_request] (D:\mongodb_test_app\node_modules\express\lib\router\layer.js:95:5) 在下一个(D:\mongodb_test_app\node_modules\express\lib\router\route.js:137:13) 在 Route.dispatch (D:\mongodb_test_app\node_modules\express\lib\router\route.js:112:3) 在 Layer.handle [as handle_request] (D:\mongodb_test_app\node_modules\express\lib\router\layer.js:95:5) 在 D:\mongodb_test_app\node_modules\express\lib\router\index.js:281:22 在 Function.process_params (D:\mongodb_test_app\node_modules\express\lib\router\index.js:335:12) 在下一个(D:\mongodb_test_app\node_modules\express\lib\router\index.js:275:10) 在 SendStream.error (D:\mongodb_test_app\node_modules\serve-static\index.js:121:7) 在 SendStream.emit (events.js:315:20) 在 SendStream.error (D:\mongodb_test_app\node_modules\send\index.js:270:17) 在 SendStream.onStatError (D:\mongodb_test_app\node_modules\send\index.js:421:12) 在下一个(D:\mongodb_test_app\node_modules\send\index.js:735:16) 在 onstat (D:\mongodb_test_app\node_modules\send\index.js:724:14) 在 FSReqCallback.oncomplete (fs.js:183:21)
【问题讨论】:
-
当你调用一个函数时,你实际上需要传递参数,否则所有参数都将是未定义的。
标签: javascript node.js mongodb express