[英]NodeJS export class cannot read property of undefined
I am currently trying to add a service layer to my NodeJS project, which is a simple API I build using express and sequelize.我目前正在尝试向我的 NodeJS 项目添加一个服务层,这是我使用 express 和 sequelize 构建的一个简单的 API。 The project has a structure of the usual model, route, and controller (MRC) form;
项目具有通常的模型、路由和控制器(MRC)形式的结构; each has its own directory.
每个都有自己的目录。 Now all I need to do is add another directory that will contain the service files.
现在我需要做的就是添加另一个包含服务文件的目录。 The files will contain the business logic in the form of a class that I will export and include in my controller files.
这些文件将以类的形式包含业务逻辑,我将导出该类并将其包含在我的控制器文件中。
Here I will display the controller and service files:在这里我将显示控制器和服务文件:
eruptionController.js eruptionController.js
const EruptionService = require('../services/eruptionService.js');
let Eruption = new EruptionService()
module.exports = {
getEruptions: (req, res) => {
Eruption.get().then(result => {
return res.status(200).json({
message: "Get All Eruptions",
result
})
}).catch(err => {
return res.status(200).json({
message: "internal Server Error",
error: err.message
})
})
}
};
eruptionService.js eruptionService.js
const { eruption } = require('../models');
class EruptionService {
constructor () {
this.eruptionModel = eruption;
}
get () {
this.eruptionModel.findAll()
.then(result => result)
.catch(err => err)
}
}
module.exports = EruptionService;
As we can see here, in the service file, I imported the eruption
model from the model folder and include it in the constructor.正如我们在这里看到的,在服务文件中,我从模型文件夹中导入了
eruption
模型并将其包含在构造函数中。 Then I created a method named get
, which purpose to retrieve all the eruption data from the database.然后我创建了一个名为
get
的方法,用于从数据库中检索所有喷发数据。
After that, I imported the EruptionService
class into the controller file, initiated it as an instance, and include the class in the getEruptions
function.之后,我将
EruptionService
类导入控制器文件,将其作为实例启动,并将该类包含在getEruptions
函数中。 The function will then be used in an express route in the form of an endpoint.然后,该函数将以端点的形式在快速路由中使用。
The issue is that when I tried to send a request to the endpoint, I got the following error问题是,当我尝试向端点发送请求时,出现以下错误
TypeError: Cannot read property 'then' of undefined
Based on the error, it seems that the EruptionService
class was not exported properly since when I called the mothed get
it's undefined.基于错误,似乎
EruptionService
类是没有,因为当我叫mothed正确导出get
它的不确定。
I've been stuck with this for a while now, and I'm not really sure what's going on.我已经被这个问题困住了一段时间,我不确定发生了什么。 Based on this post , I seem to be exporting the class properly.
基于这篇文章,我似乎正确地导出了课程。
I've also tried exporting in a way like below我也试过以如下方式导出
module.exports = {EruptionService: EruptionService}
And I've also tried importing like我也试过导入
const EruptionService = require('../services/eruptionService.js');
But in the end, it either results in the same error or different one kind of error.但最终,它要么导致相同的错误,要么导致不同的一种错误。 I was wondering if anybody could point out what I was missing?
我想知道是否有人可以指出我遗漏了什么? Thanks in advance
提前致谢
You should return a promise function for getting the then function inside it.您应该返回一个 promise 函数,用于在其中获取 then 函数。 There is a issue in your service it should be like the below snippet
您的服务存在问题,它应该类似于以下代码段
class EruptionService {
constructor () {
this.eruptionModel = eruption;
}
get () {
return this.eruptionModel.findAll()
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.