简体   繁体   中英

I get this error when running my nodejs code: ' TypeError: Cannot read property 'apply' of undefined. '

I've made a basic node.js application including a controller and a service. I keep getting this error when i try to run the application. It should be a get method for returning any table based on the passed 'namespace' and 'table' parameters.

Controller:

var express = require('express');
const testService = require ('../services/testService');

const getAnyController = async(req, res, next) => {

    var table = req.params.table;
    var namespace = req.params.namespace;
    table = table.toUpperCase();
    namespace = namespace.toLowerCase();
    var client = req.db;

    try{
        var serviceResponse = await testService.GetAny(table, namespace, client);
        res.status(200).send(serviceResponse);
        next();
    }
    catch(error){
        console.log(error.message);
        res.status(500) && next(error);
    }
}

module.exports = {
    getAnyController
}

Service:

var express = require('express');

function GetAny(table, namespace, client) {

    var getScript = 'SELECT * from ' + namespace + '.' + table;

    client.query(getScript, (err, response) => {

        if(err) {
            return 'Error: ' + err.toString();
        }
        return response;
    });
}

module.exports = {
    GetAny
};

Index.js:

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

const testController = require('../controllers/testController');

router.get("/test/:namespace/:table", (req, res) => {
    testController.getAnyController(req, res);
});

module.exports = router;

This is the error I'm getting:

/home/vcap/app/node_modules/express/lib/router/index.js:635
return fn.apply(this, arguments);
^
TypeError: Cannot read property 'apply' of undefined
at /home/vcap/app/node_modules/express/lib/router/index.js:635:15
at next (/home/vcap/app/node_modules/express/lib/router/index.js:210:14)
at Function.handle (/home/vcap/app/node_modules/express/lib/router/index.js:174:3)
at router (/home/vcap/app/node_modules/express/lib/router/index.js:47:12)
at Object.<anonymous> (/home/vcap/app/server.js:46:33)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
npm
ERR! code ELIFECYCLE
npm ERR!
errno 1
npm ERR! js@1.0.0 start: `node server.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the js@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

Found the error. The router was not defined in server.js

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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