gpt4 book ai didi

javascript - NodeJS : myFunction() is not a function

转载 作者:行者123 更新时间:2023-12-02 23:22:10 26 4
gpt4 key购买 nike

我想制作一个小型简单的 Web 服务器,其中包含可以查询的 API。所以我有一个包含以下内容的 server.js 文件:

var demandeController = require('./api/controller/DemandeController')
var http = require('http')
var express = require('express')
var app = express();

var myRouter = express.Router();

var server = http.createServer();

myRouter.route('/demandes').get((request,response)=>{
var demandes = demandeController.getAllDemandes();
response.json(demandes);
})
app.use(myRouter);
app.listen('8080')

我还有我的模块“DemandeController”,其中包含以下内容:

var sql = require('../../node_modules/mssql');
///my secret config
}

module.exports = function getAllDemandes(){
new sql.ConnectionPool(sqlConfig).connect().then(pool=>{
return pool.query('SELECT * FROM DEMANDES')
}).then(result=>{
return JSON.stringify(result);
})
}

问题是我无法将“DemandeController”模块导入到我的 server.js 文件中。

这是错误消息:

TypeError: demandeController.getAllDemandes is not a function at myRouter.route.get (C:_AureliaJS\HotlineFront&Back\HotlineAPI\server.js:11:37) at Layer.handle [as handle_request] (C:_AureliaJS\HotlineFront&Back\HotlineAPI\node_modules\express\lib\router\layer.js:95:5) at next (C:_AureliaJS\HotlineFront&Back\HotlineAPI\node_modules\express\lib\router\route.js:137:13) at Route.dispatch (C:_AureliaJS\HotlineFront&Back\HotlineAPI\node_modules\express\lib\router\route.js:112:3) at Layer.handle [as handle_request] (C:_AureliaJS\HotlineFront&Back\HotlineAPI\node_modules\express\lib\router\layer.js:95:5) at C:_AureliaJS\HotlineFront&Back\HotlineAPI\node_modules\express\lib\router\index.js:281:22 at Function.process_params (C:_AureliaJS\HotlineFront&Back\HotlineAPI\node_modules\express\lib\router\index.js:335:12) at next (C:_AureliaJS\HotlineFront&Back\HotlineAPI\node_modules\express\lib\router\index.js:275:10) at Function.handle (C:_AureliaJS\HotlineFront&Back\HotlineAPI\node_modules\express\lib\router\index.js:174:3) at router (C:_AureliaJS\HotlineFront&Back\HotlineAPI\node_modules\express\lib\router\index.js:47:12)

感谢您的帮助

最佳答案

尝试将 getAllDemandes() 函数放入变量中,并使用 module.exports 导出该变量而不是整个函数

关于javascript - NodeJS : myFunction() is not a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56904086/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com