我有一个数组。在那个数组中是对象。在每个对象中,我都希望理想地添加一个布尔值,但连接是一对多的,因此这就是标题向对象添加子数组的原因。让我们举一个例子-我想看看约翰是否有任何可用的测试。约翰可以进行一百次测试,但我只想拥有一个正确或错误的标志。我正在使用node.js express mssql模块。
route.js
router.get('/', (req, res) => {
connectPool.then(pool => {
let sqlString = `
SELECT * from table where values
`
return pool.request().input(values here).query(sqlString)
}).then(result => {
// console.log(result[1], 'logging result')
let newResult = result.recordset
newResult.forEach(result => {
result.id = getTests(result.id)
})
res.status(200).json(newResult);
sql.close();
}).then().catch(err => {
res.status(500).send({ message: 'Server Error'})
sql.close();
});
})
function getTests(id) {
connectPool.then(pool => {
let sqls = `
SELECT *
FROM table
WHERE table.id = @input_parameter
`
// console.log(pool, 'logging pool')
return pool.request().input('input_parameter', sql.Int, id).query(sqls)
}).then(result => {
let rows = result.recordset[0]
// res.status(200).json(rows);
// sql.close();
return connectPool
}).catch(err => {
res.status(500).send({ message: 'Server Error'})
});
}
第一部分有效。如果通过上面的示例进行分析,我可以获得所有名称。似乎无法通过getTests函数建立与数据库的连接。它未连接,因为我返回的结果不确定。就像我说的那样,第一部分很好,只需要向对象添加一个标志或一个数组即可,但是我不能把那部分记下来。谢谢。