Javascript TypeError:函数x不是函数
我定义了以下函数:Javascript TypeError:函数x不是函数,javascript,node.js,promise,typeerror,backend,Javascript,Node.js,Promise,Typeerror,Backend,我定义了以下函数: const findSocketForUser = userId => { console.log("IM HERE "); const userSocketId = socketToUserMap[userId].socketId; console.log(userSocketId); return connections.find(x => x.id == userSocketId); }; 接下来,我在这里导出函数: module.exp
const findSocketForUser = userId => {
console.log("IM HERE ");
const userSocketId = socketToUserMap[userId].socketId;
console.log(userSocketId);
return connections.find(x => x.id == userSocketId);
};
接下来,我在这里导出函数:
module.exports = {
io,
...,
findSocketForUser,
...
};
我将其导入另一个文件:
const { findSocketForUser, io } = require("./../../socket");
我试着如下使用它:
const newFunction = id => {
Model1.findOne({
where: {
id: id
},
include: [{ model: Model2 }]
})
.then(result => {
if (
...
) {
Model1.update(
{
...
},
{ where: { id: id } }
)
.then(_ => {
Model3.create({
...,
})
.then(_ => {
newFunction2(
result.field1.field2,
id,
result.winner,
result.field3
);
})
.catch(error => {
console.log(error);
});
})
.catch(error => {
console.log(error);
});
} else {
console.log("err");
}
})
.catch(error => {
console.log(error);
});
};
其中newFunction2
是:
const newFunction2 = (field1, field2, field3, field4) => {
if (field1) {
const socket = findSocketForUser(field1);
...
}
};
这会引发一个错误:
TypeError: findSocketForUser is not a function
at newFunction2 (/...:71:20)
我还注意到findSocketForUser
中的console.log
从未执行过。我确保导入的路径正确,函数名中没有输入错误,传递到newFunction2
的变量都不为空。问题是什么
更新:
const {
Model1,
Model2,
Model3
} = require("../../../db/sequelize");
const { findSocketForUser, io } = require("./../../socket");
const {
MESSAGE1,
MESSAGE2
} = require("./../../messages");
你有一个循环引用。您需要文件B中的文件A,反之亦然。这就是为什么当您使用
require
时会得到{}
最简单的解决方案是使用第三个文件中断循环引用
也请检查此问题:
console.log(findSocketForUser)
在您需要它之后。展示output@MarcosCasagrande它记录undefinedNow:console.log(require(“../../../socket”)
并用两个文件的所有代码更新问题,我需要查看您正在执行的要求。@MarcosCasagrande它记录{}我知道您的问题,用代码更新问题,这样我就可以确定了。