将类导入另一个模块时,我遇到了一个非常奇怪的问题。在我的start.js文件中,我可以导入worker模块,它可以很好地运行脚本。但是,当它进入套接字模块并尝试导入工作程序模块时,出现以下错误。
当我在socket.js构造函数中控制台日志工作程序时,它显示为空对象。当我在start.js中进行控制台登录时,它显示为一个函数。
我知道我必须忽略这很简单的东西,但我看不到。
this.Worker = new worker()
^
TypeError: worker is not a constructor
at new Socket (C:\Users\***\Desktop\***\src\modules\socket.js:7:17)
at new Worker (C:\Users\***\Desktop\***\src\modules\worker.js:6:17)
at Object.<anonymous> (C:\Users\***\Desktop\***\start.js:2:16)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
at startup (bootstrap_node.js:188:16)
我有一个 start.js 文件,我在其中导入工作程序模块并启动我的应用程序。
const worker = require('./src/modules/worker')
const Worker = new worker()
Worker.start()
worker模块( worker.js )导入我的套接字服务器模块并连接到套接字服务器。
const socket = require('./socket')
class Worker {
constructor() {
this.Socket = new socket()
}
async start() {
try {
console.log('ran start')
// Connect to socket server
await this.Socket.connect()
} catch(err) {
console.log('Unable to connect to socket server.', err)
}
}
startJob(work) {
const jobType = work.work.jobType
const jobData = work.work.jobData
// run module based on jobType
}
}
module.exports = Worker
我的套接字服务器模块( socket.js )也导入(worker.js),以便我可以在socket.on('process')事件中将startJob函数用作Worker.startJob( )。
const worker = require('./worker');
class Socket {
constructor() {
this.socket = require('socket.io-client')('http://localhost:3000')
this.Worker = new worker()
}
async connect() {
try {
// Connect to socket server
await this.connectServer()
} catch(err) {
console.log('Unable to start worker.', err)
}
}
async connectServer() {
let workerId;
try {
// Connection to socket server established
this.socket.on('connect', () => console.log('Connected to socket server.'))
// Get socketId for job issuance
this.socket.on('id', id => { workerId = id })
// Receive new work orders
this.socket.on('process', async work => {
const myId = work.id
// Check if job was assigned to my socketId
if (myId == workerId) {
console.log('starting job')
// await this.Worker.startJob(work)
}
})
} catch(err) {
console.log('Unable to connect to socket server.', err)
}
}
}
module.exports = Socket
答案 0 :(得分:1)
模块和使用export/import
module.exports
对象是由Module
系统创建的
因此,当您将module.exports = Worker
,Worker
添加到module.export
对象时,意味着它是对象中的key
。
您在代码中require
的位置:
const worker = require('./src/modules/worker')
// class Worker is available as a member
const Worker = new worker.Worker()
如果您不希望这样做,还有另外两种方法可以解决:
// note the curly braces and capitalized W because JavaScript is case sensitive
const { Worker } = require('./src/modules/worker');
或者您可以通过以下方式对您的课程进行替换 module.exports
:
module.exports = class {
constructor() {
this.Socket = new socket()
}
async start() {
try {
console.log('ran start')
// Connect to socket server
await this.Socket.connect()
} catch(err) {
console.log('Unable to connect to socket server.', err)
}
}
startJob(work) {
const jobType = work.work.jobType
const jobData = work.work.jobData
// run module based on jobType
}
}
并以类似方式导入:(注意,您可以将其命名为其他名称)
const Worker = require('./worker')
答案 1 :(得分:0)
由于没有worker
类而有Worker
类(区分大小写),因此触发了错误。