x不是nodejs的构造函数

时间:2018-04-08 19:55:00

标签: javascript node.js express

我有三个文件和index.js,这是需要和导出的代码

index.js:

'use strict'
const RemoteService = require('./src/RemoteService.js');
const remoteService = new RemoteService();

RemoteService.js:

'use strict'
const MoviesService = require('./MoviesService.js');
const AiService = require('./AiService.js');
const aiService = new AiService();
const moviesService = new MoviesService();

AiService.js

const MoviesService = require('./MoviesService.js');
const RemoteService = require('./RemoteService.js');
const moviesService = new MoviesService();
const remoteService = new RemoteService();

MoviesService.js

'use strict'
const RemoteService = require('./RemoteService.js');
const remoteService = new RemoteService();

我收到错误 TypeError:RemoteService不是构造函数 at:at Object。 (代码路径\ SRC \ MoviesService.js:9:23)

我做得对,是否有更好的方法来使用这些对象?

编辑

index.js:

'use strict'
const RemoteService = require('./src/RemoteService.js').RemoteService;
const MoviesService = require('./src/RemoteService.js').moviesService;
const AiService = require('./src/RemoteService.js').aiService;
const remoteService = new RemoteService();

RemoteService.js:

'use strict'
const MoviesService = require('./MoviesService.js').MoviesService;
const AiService = require('./AiService.js').AiService;
const aiService = exports.aiService = new AiService();
const moviesService = exports.moviesService = new MoviesService();

AiService.js

const MoviesService = require('./MoviesService.js').MoviesService;
const RemoteService = require('./RemoteService.js').RemoteService;
const moviesService = exports.moviesService = new MoviesService();
const remoteService = exports.remoteService = new RemoteService();

MoviesService.js

'use strict'
const RemoteService = require('./RemoteService.js').RemoteService;
const remoteService = exports.remoteService = new RemoteService();

我仍然得到错误 TypeError:RemoteService不是构造函数 at:at Object。 (代码路径\ SRC \ MoviesService.js:9:23)

0 个答案:

没有答案