Tuesday, 6 June 2017

Can someone help me with this Promise issue in Node.js?

I'm getting an unresolved Promise. What am I missing? Thx!

I have this function:

export async function readData(dir) {
await fs.readFile(dir, `utf-8`, (err, data) => {
    if (err) throw err
    console.log("data1:", data)
    return data
})
}

Which I'm calling from this function:

app.get('/server/survey', (ctx) => {
let dir = path.join(__dirname, 'src/DB/index.json')
let data = funcs.readData(dir)
data.then((result) => {
    console.log("Content:", result)
    ctx.body = `${result}` 
})

The first one logs out the data correctly but the second logs out an unresolved Promise. I'm missing the resolution. Thanks for any help.



via Fedreg

No comments:

Post a Comment