[英]NodeJs. Problem with module.export; it is not a function error
I have written this piece of code:我写了这段代码:
function newExercise(data){
pool.query('INSERT INTO Exercises VALUES(?, ?)', [data, data], function(error, results, field){
if(error) console.log(error);
})
}
module.exports = {
newExercise: newExercise,
}
But when I call the function from here:但是当我从这里调用函数时:
Promise.all([exname, exdesc])
.then( values => {
router.get('/', function(req, res, next){
var socket = req.app.get('socket');
io = req.app.get('socketio');
io.sockets.on('connection', function(socket){
socket.on('message', function(message){
console.log("Ricevuto");
database.newExcercise(message);
})
})
res.render('exercises', {title: 'Exercises', ex: values[0]});
})
})
.catch(error => console.error(error));
I get "TypeError: database.newExcercise is not a function", but I don't understand why我收到“TypeError: database.newExcercise is not a function”,但我不明白为什么
In the above code snippet, you defined the function as: newExercise
, and in the second snippet, you're calling it as newExcercise
.在上面的代码片段中,您将函数定义为:
newExercise
,而在第二个片段中,您将其称为newExcercise
。
It's a simple typo.这是一个简单的错字。
对我来说,解决方案是删除循环引用。
尝试以下操作: module.exports.newExercise = newExercise;
file name NewExercise.js文件名 NewExercise.js
module.exports = function newExercise(data){
pool.query('INSERT INTO Exercises VALUES(?, ?)', [data, data], function(error, results, field){
if(error) console.log(error);
})
}
in another file where you are calling this function you just var newExercise= require('./NewExercise.js');
在您调用此函数的另一个文件中,您只需
var newExercise= require('./NewExercise.js');
and you just call it newExercise(message)
你就叫它
newExercise(message)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.