Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 获取错误-Can';t集';标题';未定义的_Javascript_Node.js_Mongodb_Express - Fatal编程技术网

Javascript 获取错误-Can';t集';标题';未定义的

Javascript 获取错误-Can';t集';标题';未定义的,javascript,node.js,mongodb,express,Javascript,Node.js,Mongodb,Express,我遇到了这个错误-无法设置未定义的的标题 代码引用 单独定义的标题: var headers = function (req,res,next){ res.setHeader("Access-Control-Allow-Origin", "[*]"); res.setHeader("Access-Control-Allow-Headers","Origin, X-Requested-With, Content

我遇到了这个错误-
无法设置未定义的
的标题

代码引用


单独定义的标题:

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: 


详细错误:

TypeError:无法读取未定义的属性“setHeader” at标题(D:\mongodb\u test\u app\app-module-1.js:231:7) 在fn1(D:\mongodb\u test\u app\app-module-1.js:303:7) 在Layer.handle[作为handle\u请求](D:\mongodb\u test\u app\node\u modules\express\lib\router\Layer.js:95:5) 下一步(D:\mongodb\u test\u app\node\u modules\express\lib\router\route.js:137:13) 在Route.dispatch(D:\mongodb\u test\u app\node\u modules\express\lib\router\Route.js:112:3) 在Layer.handle[作为handle\u请求](D:\mongodb\u test\u app\node\u modules\express\lib\router\Layer.js:95:5) 位于D:\mongodb\u test\u app\node\u modules\express\lib\router\index.js:281:22 在Function.process_参数处(D:\mongodb_test_app\node\u modules\express\lib\router\index.js:335:12) 下一步(D:\mongodb\u test\u app\node\u modules\express\lib\router\index.js:275:10) 在SendStream.error(D:\mongodb\u test\u app\node\u modules\serve static\index.js:121:7) 在SendStream.emit(events.js:315:20) 在SendStream.error(D:\mongodb\u test\u app\node\u modules\send\index.js:270:17) 位于SendStream.onStatError(D:\mongodb\u test\u app\node\u modules\send\index.js:421:12) 下一步(D:\mongodb\u test\u app\node\u modules\send\index.js:735:16) 在onstat(D:\mongodb\u test\u app\node\u modules\send\index.js:724:14) 在FSReqCallback.oncomplete(fs.js:183:21)

你的电话是:

headers();

但是,所有这些函数都需要几个参数。您需要传递这些参数:

headers(req, res, next);
middleware1(req, res, next);
middleware2(req, res, next);

从技术上讲,
headers
函数实际使用的唯一参数是
res
,因此您可以将其定义更改为只有一个参数
res
,并只传递该参数。

调用函数时,您实际上需要传递参数,否则所有参数都将未定义。您好@jfriend00。。。。我做了以下更新。但也没有成功。你能推荐一些其他的方法吗?@SuchetaShrivastava-嗯,你也在调用
middleware1()
middleware2()
因此也不要将参数传递给这些参数。说真的,如果调用方没有在所有代码中传递参数,就不能使用参数。我们已经向你们指出了这一点。我们不必在您的代码中找到所有这样的实例——您应该能够自己做到这一点。此外,当您遇到这样的错误时,使用
console.log()
或在调试器中检查参数是什么也是基本调试。在来这里之前,你应该早就自己动手了。这是基本的调试…谢谢。我试试看this@SuchetaShrivastava-注意,修复此问题时会产生其他问题,因为您尝试在
middleware1
fn1
中多次调用
next()
。不能那样做。这段代码有很多错误。您正试图通过调用
next()
来处理表示完成的异步函数,就像它们是同步的,不会工作一样。您好@jfriend00。。。。当我在-->app.get('/2498',..)--->中注释next()时,我得到了这个错误-->类型错误:
executeLegacyOperation
的最后一个参数必须是回调
  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);

                             
              })
              })
              ```
headers();
middleware1();
middleware2();
headers(req, res, next);
middleware1(req, res, next);
middleware2(req, res, next);