繁体   English   中英

Node.js。 module.export 有问题; 这不是函数错误

[英]NodeJs. Problem with module.export; it is not a function error

我写了这段代码:

function newExercise(data){
    pool.query('INSERT INTO Exercises VALUES(?, ?)', [data, data], function(error, results, field){
        if(error) console.log(error);
    })
}

module.exports = {
    newExercise: newExercise,
}

但是当我从这里调用函数时:

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));

我收到“TypeError: database.newExcercise is not a function”,但我不明白为什么

在上面的代码片段中,您将函数定义为: newExercise ,而在第二个片段中,您将其称为newExcercise

这是一个简单的错字。

对我来说,解决方案是删除循环引用。

尝试以下操作: module.exports.newExercise = newExercise;

文件名 NewExercise.js

module.exports = function newExercise(data){
   pool.query('INSERT INTO Exercises VALUES(?, ?)', [data, data], function(error, results, field){
      if(error) console.log(error);
   })
}

在您调用此函数的另一个文件中,您只需var newExercise= require('./NewExercise.js'); 你就叫它newExercise(message)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM