Javascript 带有MariaDB的节点,无法读取属性';threadId';未定义的
因为这个恼人的错误,我非常绝望。我在网上找不到任何有关这方面的信息 实际上,我的应用程序工作正常,但是如果我启动多个查询并使用Javascript 带有MariaDB的节点,无法读取属性';threadId';未定义的,javascript,node.js,promise,runtime-error,mariadb,Javascript,Node.js,Promise,Runtime Error,Mariadb,因为这个恼人的错误,我非常绝望。我在网上找不到任何有关这方面的信息 实际上,我的应用程序工作正常,但是如果我启动多个查询并使用Promise.all()等待它们,服务器就会崩溃。但一步一步: 在我的index.js中,我用最多60个连接初始化连接池,并导出变量 const pool = mariadb.createPool(config.mariaDB); module.exports.pool = pool 稍后,我将索引文件导入为“app”,并使用app.pool.query(…)来查
Promise.all()
等待它们,服务器就会崩溃。但一步一步:
在我的index.js中,我用最多60个连接初始化连接池,并导出变量
const pool = mariadb.createPool(config.mariaDB);
module.exports.pool = pool
稍后,我将索引文件导入为“app”,并使用app.pool.query(…)来查询我的数据库
接下来,我有一个post请求/getUsersGroups
。一个用户可以是多个组的成员,因此我希望响应是一个对象数组。每个对象都包含有关组的信息。此外,此对象中还有一个成员字段。此字段也是一个数组,包含有关成员的信息,因此s.th。就像这样:
[
{
"groupId": 125758,
"title": "test",
"members": [
{userId: 5, name:Max, }, ...]
}, ...
]
因此我有一个post请求,看起来是这样的。我正在获取用户所属的所有组ID。然后,对于每个groupId
,我调用方法Group.groupInfo.
这将返回一个承诺,因此我将收集数组中的所有承诺,并在发送响应之前等待所有承诺
router.post("/getUsersGroups", (req, res) => {
let userId = req.body.userId;
let result = []
let promises = []
Group.getGroupsByUser(userId) // this gets all group ids the user is a member
.then((item) => {
let groups = item.map(x => objectParser.parseUnderscoreToCamel(x))
for (let i = 0; i < item.length; i++) {
let groupId = item[i].groupId
promises.push( //adding promises
Group.groupInfo(groupId, userId)
.then((item) => {
result.push(item)
})
.catch((err)=>{console.log(err)}))
}
})
.then(() => {
// waiting for all promises to finish
return Promise.all(promises)
.then(() => {
res.status(200).json({groups: result})
})
})
.catch((err) => {
console.log(err)
}
})
}
User.userInfo
本身对数据库进行查询,以收集有关用户的信息
如果我调用此命令,服务器将崩溃并出现以下错误:
activeConnections[conn.threadId] = conn;
TypeError: Cannot read property 'threadId' of undefined
at Pool.handleTaskQueue (****\node_modules\mariadb\lib\pool.js:431:30)
at _combinedTickCallback (internal/process/next_tick.js:132:7)
at process._tickCallback (internal/process/next_tick.js:181:9)
我猜问题出在某种程度上与承诺有关。all()
对吗?
但是我找不到关于这个错误的任何信息。我感谢每一个提示
我的解决办法不是使用承诺,而是依次调用承诺,但对我来说似乎不正确
return groupsId.reduce((promise, groupId) => {
return promise.then(() => {
return Group.groupInfo(groupId, userId)
.then((item)=>{
result.push(item)
console.log(result)
})
.catch((err)=>{
throw err
})
})
}, Promise.resolve())
我不能相信这一点,只要我不理解错误
谢谢
return groupsId.reduce((promise, groupId) => {
return promise.then(() => {
return Group.groupInfo(groupId, userId)
.then((item)=>{
result.push(item)
console.log(result)
})
.catch((err)=>{
throw err
})
})
}, Promise.resolve())