My exports function isn't working because of error "TypeError: Cannot read properties of undefined (reading 'send')"

60 Views Asked by At

I'm new to the server side of programming and I can't figure out why the terminal is telling me that the send function in express isn't defined. I've tried putting express in the document, and I've tried different formats for the function, but I still can't figure it out. (It's complaining about the "getgameData" function)

I changed the send function thinking that it might be that you can't use the send function, but it turns out that no functions work there, but when I just try and return any other data structure, the terminal is still upset. Then, I tried restructuring the function and it still didn't work.

/////////////////////////////////////////////////////////////////////////////////////////

    var gameData = require('../models/gameData.js');

    var player = "ed";

    exports.updateGame = function(res,req){
    gameData.gameData = req.data;
    }

    exports.resetGame = function(){
    res.send(gameData.gameData);
    }

    exports.getGameData = function(res,req){
    gameData.gameData.playerNames.me = player;
    res.setHeader('Content-Type','text/plain');
    res.send(gameData.gameData.playerNames.me);
    }

////////////////////////////////////////////////////////////////////////////////////// ** The app that connects the client side and the server side ////////////////////////////////////////////////////////////////////////////////////// //all required js for Part 2 var game = require('./controllers/gameController'); var setup = require('./controllers/setupController'); var topTen = require('./controllers/topTenController');

// all routes

// game data routes
app.route('/api/gameData')
.get(game.getGameData())
.patch(game.updateGame)
1

There are 1 best solutions below

2
Brettski On

Pass the callback function into the get(), not the function, executed. Get will pass in req, res, next to your callback and then execute it.

// all routes

// game data routes
app.route('/api/gameData')
.get(game.getGameData)
.patch(game.updateGame)

I also just noticed that you have your middleware functions arguments in the wrong order. The order is: req, res, next. Reference here.