我正在尝试将数据发布到我的本地 sqlite 数据库。数据在 POST 后出现在我的数据库表中,但Promise.resolve()返回 as undefined,这又不会将结果返回给客户端。我错过了什么吗?
任何帮助,将不胜感激。这是我得到的。
module.exports.addAccount = function (data) {
const db_conn = new sqlite3.Database( path.join(__dirname, "../user_database.sql") )
return new Promise( function (resolve, reject) {
db_conn.serialize( function () {
// insert row
db_conn.run("INSERT INTO some_table (username) VALUES (?);", [data.username], function (err,rows) {
if (!err) {
console.log(rows) // always returns undefined
resolve([rows, this.lastID])
} else {
reject(err)
}
})
})
db_conn.close()
})
}
然后:
app.post("/add-row", function (req, res) {
user_info.addAccount(req.body).then( function(response) {
res.json({
rows: response[0], // this is undefined
row_id: response[1] // this is not
})
}).catch(function () {
})
})
慕后森
相关分类