0
const con = require('./../connection');
 exports.getProfile = async function(req,res){

        var user_id    = req.session.user_id;
        var userList   = '';
        var id         = req.params.id ? req.params.id  : '';
        if(!id)
        {
          id = user_id;
        }
        await con.query('select * from users where id = ?',[id],function(error,results,fields){

            res.render('profile',{
              userResult: results[0],
            });
    });
};

<input type="text" name="f_name" data-required="1" value="<%= userResult.first_name %>" class="form-control">

getting correct response on web but getting error in console Cannot read property 'first name' of undefined nodejs

2 Answers 2

1

I think you have a synchronization issue there.

You're trying to access userResult before the function callback ends.

I don't know what you're trying to accomplish there but the value you're trying to access will be available here:

 await con.query('select * from users where id = ?',[id],function(error,results,fields){
           // HERE YOU CAN ACCESS TO results[0]

            res.render('profile',{
              userResult: results[0],
            });
  });
Sign up to request clarification or add additional context in comments.

5 Comments

I am getting error in EJS file. I think value="<%= userResult.first_name %>" is creating problem does above code solve this issue ?
Hey @Sam, the code above serve as example as where you can access your results[] without further complexity. As far as I can understand here you're trying to access "userResult.first_name" right? if that's the case you can access that value where I mentioned using results[0].first_name
Yes it does output when we do console.log(result[0].first_name) but it does work for me when i apply this in ejs file <% if (typeof userResult == 'object' && userResult) { %>
even i don't understand you only mentioned await not async ?
I only quoted that part of your code to serve as an example. My intention was to point out that you can access the required data inside the mentioned callback
0

If you wish to access the data outside of the module you'll need to do something like so:

exports.getProfile = async function(req,res){

    var user_id    = req.session.user_id;
    var userList   = '';
    var id         = req.params.id ? req.params.id  : '';
    if(!id)
    {
        id = user_id;
    }

    return new Promise((resolve, reject) => {
        con.query('select * from users where id = ?', [id], function(error, results, fields) {
            if (error) {
                reject(error);
            } else {
                console.log("First name: ", results[0].first_name);
                res.render('profile',{
                    userResult: results[0],
                });
                resolve(results[0]);
            }
        });
    })

};

And then in your importing script:

const { getProfile } = require('./get-profile');

async function testGetProfile() {
    let profile = await getProfile(req, res);
    console.log("Profile: First name: ", profile.first_name);
}

// Pull req, res from relevant route..
testGetProfile(req, res);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.