I'm having a really weird issue with importing my class into another module. In my start.js file, I can import the worker module and it runs the script fine. However, when it gets to the socket module and tries to import the worker module, I get the following error.
When I console log worker in the socket.js constructor, it shows as an empty object. When I console log it in the start.js, it shows as a function.
I know this has to be something very simple that I'm overlooking, but I just can't see it.
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._pile (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)
I have a start.js file where I import my worker module and start my app.
const worker = require('./src/modules/worker')
const Worker = new worker()
Worker.start()
The worker module (worker.js) imports my socket server module and connects to the socket server.
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
My socket server module (socket.js) also imports (worker.js) so that I can use the startJob function within the socket.on('process') event as 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
I'm having a really weird issue with importing my class into another module. In my start.js file, I can import the worker module and it runs the script fine. However, when it gets to the socket module and tries to import the worker module, I get the following error.
When I console log worker in the socket.js constructor, it shows as an empty object. When I console log it in the start.js, it shows as a function.
I know this has to be something very simple that I'm overlooking, but I just can't see it.
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._pile (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)
I have a start.js file where I import my worker module and start my app.
const worker = require('./src/modules/worker')
const Worker = new worker()
Worker.start()
The worker module (worker.js) imports my socket server module and connects to the socket server.
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
My socket server module (socket.js) also imports (worker.js) so that I can use the startJob function within the socket.on('process') event as 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
Share
Improve this question
asked Apr 9, 2019 at 0:08
Jayson HJayson H
2,0585 gold badges22 silver badges30 bronze badges
2 Answers
Reset to default 4There are couple of things to note with modules and using export/import
The
module.exports
object is created by theModule
system
So when you do, module.exports = Worker
, Worker
is added to the module.export
object meaning it's a key
in the object.
Where you require
in your code:
const worker = require('./src/modules/worker')
// class Worker is available as a member
const Worker = new worker.Worker()
If you don't want this, there's two other way you could do:
Destructuring assignment
// note the curly braces and capitalized W because JavaScript is case sensitive
const { Worker } = require('./src/modules/worker');
Or you can replace module.exports
with your class this way:
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
}
}
And import like: (Note, You can name it to anything else)
const Worker = require('./worker')
The error is triggered because you don't have a worker
class you have a Worker
class (it's case-sensitive).
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745136771a4613240.html
评论列表(0条)