0 votes
1 view
in Web Technology by (11.2k points)

In my user.js file I have a async function to retrieve the avatar location from the database as follows:

async findavatar(username) {

        const sql = `SELECT image FROM users WHERE user = "${username}";`

        const db = await sqlite.open(dbName)

        const location = await this.db.get(sql)

        await db.close()

        console.log(location)

        return location

    }

I call it in the index.js file:

router.get('/', async ctx => {

    try {

        if(ctx.session.authorised !== true) return ctx.redirect('/login?msg=you need to log in')

        const data = {}

        const user = await new User(dbName)

        const currentuser = ctx.session.loggeduser

        const avatarlocation = user.findavatar(currentuser)

        console.log('location of avatar:' + avatarlocation)

        if(ctx.query.msg) data.msg = ctx.query.msg

        await ctx.render('newarticle', {location: currentuser })

    } catch(err) {

        await ctx.render('error', {message: err.message})

    }

})

The console.log in user file works and displays the location but when I get to the index.js file and run it gives a promise. When I run the page to retrieve the location I get [object Promise] as a result, how do I get it to show the value pulled from the database?

1 Answer

0 votes
by (22.1k points)

The Promise object represents the eventual completion (or failure) of an asynchronous operation, and its resulting value.

You need to call .then() to resolve the Promise.

Example:

avatarlocation.then(function(value) {

    console.log(value);

});

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...