[英]Function returning undefined when exported
我在 node.js 中創建了一個模塊,它有 2 個函數 - takeInput 和 getEventEmitter。 這兩個都是出口的。 但是當我需要它是其他一些文件時,takeInput 工作正常但 getEventEmitter 結果未定義。
以下是代碼:-
// main module.js
function takeInput(db) {
// logic to take input from user
}
function getEventEmitter(db) {
const eventEmitter = new EventEmitter();
console.log(takeInput);
eventEmitter.on('function execution complete', () => takeInput(db));
eventEmitter.emit('function execution complete');
}
module.exports = {
takeInput,
getEventEmitter
}
導出 main module.js 的模塊
const { getEventEmitter } = require('main module');
// Some lines of code ...
getEventEmitter(db); // Error here when this function is called.
錯誤如下
TypeError: getEventEmitter is not a function
請幫忙。
您需要從 main module.js 導出這兩個函數
function takeInput(db) {
// logic to take input from user
}
function getEventEmitter(db) {
const eventEmitter = new EventEmitter();
console.log(takeInput);
eventEmitter.on('function execution complete', () => takeInput(db));
eventEmitter.emit('function execution complete');
}
export { takeInput, getEventEmitter }
然后它會起作用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.