NodeJS sqlite3 如何只返回列中的值,而不是列数据


router.get('/play/:song_id', async ctx => {
    try {
        const sql = `SELECT location FROM songs WHERE song_id = ${ctx.params.song_id} LIMIT 1;`
        const db = await sqlite.open(dbName)
        const data = await db.get(sql)
        await db.close()
        const newdata = JSON.parse(JSON.stringify(data))
        console.log(newdata)
        ctx.response.type = 'mp3'
        ctx.response.body = fs.createReadStream(newdata)
    } catch (err) {
        ctx.body = err.message
    }
})

这是我得到的输出:

{ location: 'public/songs/Santeria.mp3' }

但我只想要'public/songs/Santeria.mp3'部分(列的值(。

你只需要改变这一行:

ctx.response.body = fs.createReadStream(newdata)

ctx.response.body = newdata.location

最新更新