TypeError:无法读取未定义的属性“ apply”?

时间:2020-03-03 07:59:20

标签: node.js

设置路由器文件时。 我得到

TypeError: Cannot read property 'apply' of undefined at /Users/evibreukers/Desktop/NODEJS/beginnerguide/node_modules/express/lib/router/index.js:635:15 
at next (/Users/evibreukers/Desktop/NODEJS/beginnerguide/node_modules/express/lib/router/index.js:210:14) 
at Function.handle (/Users/evibreukers/Desktop/NODEJS/beginnerguide/node_modules/express/lib/router/index.js:174:3) 
at router (/Users/evibreukers/Desktop/NODEJS/beginnerguide/node_modules/express/lib/router/index.js:47:12) 
at Object. (/Users/evibreukers/Desktop/NODEJS/beginnerguide/server.js:20:20) 
at Module._compile (internal/modules/cjs/loader.js:1158:30) 
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10) 
at Module.load (internal/modules/cjs/loader.js:1002:32) 
at Function.Module._load (internal/modules/cjs/loader.js:901:14) 
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)

问题在快递库中。

我不确定问题在代码中的什么地方,因为我自己没有使用apply方法。

/router/index.js

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

router.get('/',function(req,res){
    res.render('index.html')
});

router.get('/about',function(req,res){
    res.render('about.html');
});

router.get('/all', function (req, res) {
    res.send(projectData);
    // console.log(req);       
});

module.exports.data = data = []

router.post('/addName', function (req, res) {
    data.push(req.body);
    console.log(data);      
});

module.exports = router;

server.js

projectData = {};

// --> npm init (set up package.json)

// --> npm install express
const express    =    require('express');
const app        =    express();

app.use(express.static(__dirname + '/static')); 

// --> npm install cors 
const cors = require('cors');
app.use(cors());

// --> npm install ejs
require('./routes')(app); 
app.set('views',__dirname + '/views'); 
app.set('view engine', 'ejs'); 
app.engine('html', require('ejs').renderFile); 

// --> npm install body-parser 
const bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

// connect with routes file
app.use(require('./routes'));

// set up PORT
const port     =    3000;
const server   =    app.listen(port,function(){
    console.log(`We have started our server on port ${port}`);
});

/static/app.js

const postData = async ( url = '', data = {})=>{                 
    console.log(data);                                           
    const response = await fetch(url, {                        

        method: 'POST', 
        credentials: 'same-origin',
        headers: {'Content-Type': 'application/json',},
        // Body data type must match "Content-Type" header        
        body: JSON.stringify(data),                               

    }); // end of respnose

    try {
        const newData = await response.json();                  
        console.log(newData);
        return newData;                                         
    } 

    catch(error) {
        console.log("error", error);
    }

} // end of postData

postData('/addName', {answer: 'evi'});                                  

2 个答案:

答案 0 :(得分:0)

回溯指向require('./routes')(app);行(如果我正确地计算了行号)。

我认为您可以删除它,因为稍后要进行app.use(require('./routes'));

答案 1 :(得分:0)

因为您需要带有 require('./routes')(app); 的模块,所以在您的模块中,您需要导出一个将 app 作为参数的函数:

module.exports = app => {
    ...
    return router;
}

因此,该错误可能是因为您将参数传递给不希望有参数的东西。