1

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

0

4 Answers 4

2

In the above code snippet, you defined the function as: newExercise, and in the second snippet, you're calling it as newExcercise.

It's a simple typo.

Sign up to request clarification or add additional context in comments.

Comments

2

For me the solution was removing a circular reference.

Comments

1

Try the following: module.exports.newExercise = newExercise;

Comments

0

file name 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'); and you just call it newExercise(message)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.