akhilchoudhary2k / MindMatch-kgp

An application for connecting Like Minded Students
https://mindmatchkgp.herokuapp.com/
3 stars 9 forks source link

Show first name & last name in connections list on home page #34

Open akhilchoudhary2k opened 2 years ago

akhilchoudhary2k commented 2 years ago

Note: Show NA if the value is unavailable

see image for reference.

Screenshot 2022-10-27 at 1 58 04 AM
RandomUserWithInternet commented 2 years ago

Could I take this issue?

akhilchoudhary2k commented 2 years ago

Yes pls go ahead @RandomUserWithInternet , but try to raise PR ASAP other wise I may reassign it. As it is an easy fix.

RandomUserWithInternet commented 2 years ago

Trying to access the fname and lname data by looking for other users via connections, but I don't know where user data of everyone else on the site is located.

akhilchoudhary2k commented 2 years ago

in app.get("/UserHome" we do res.render('UserHome', {user: req.user}); then in UserHome.ejs we get the name of ith connection from user.connections[i]

it means we don't pass the fname & lname information, user.connections[i] stores only the username. maybe you can pass a maping of username --> {fname, lname} or pass 2 lists fname_list & lname_list.

@RandomUserWithInternet in short: the implementation depends on you, but you need to query the DB to get fname & lnames then pass it, that's it.

RandomUserWithInternet commented 2 years ago

I don't think I'm familiar with the DB syntax enough to solve this issue. I understand that only the username is available, but I'm just not sure how to use it to look up in the database the fname/lname it's connected to and pull the necessary information. Sorry about that.

RandomUserWithInternet commented 2 years ago

Pretty new to GitHub myself, so IDK if there's a way to unassign myself or not. I'll let you deal with it ig lol.

akhilchoudhary2k commented 2 years ago

@RandomUserWithInternet Will you be able to do it if I create a function to get fname & lname from username which does the DB query ??

roughly DB query will be like :

User.findOne({
                username: username
            }, function(err, found) {
            if(found) {
                   return found.fname, found.lname
            }
});
RandomUserWithInternet commented 2 years ago

Yeah I'll try it out.

RandomUserWithInternet commented 2 years ago

app.js image UserHome.ejs image Here's what I did... Not sure if I implemented it correctly.

akhilchoudhary2k commented 2 years ago

you are on the right lines, but it's not correct wrt the implementatin will help you fix it tomorrow.

RandomUserWithInternet commented 2 years ago

Thanks! I appreciate you helping me out, in spite of my incompetence.

RandomUserWithInternet commented 2 years ago

Just taking a second look, I'm assuming I need to return the User.findOne within the function as well. Also, what will it return if it is empty? Would it be null or something else?

akhilchoudhary2k commented 2 years ago

@RandomUserWithInternet --> it will return null if empty, so replace that with "NA"

--> create a list connectionsFullName and iterate on connections and append the [fname, lname] into this connectionsFullName list and pass it to res.render()

--> this getNames function may not run in order because it contains a time costly DB operation, you need to take care of synchronization. link for reference: LINK

RandomUserWithInternet commented 2 years ago

Alright, I'll try and implement those things. Could you explain to me what res.render() is? I see it elsewhere in the app.js file but don't know what exactly it does.

akhilchoudhary2k commented 2 years ago

Refer to internet and documentation. But in a nut shell res.render( x, {par1:val1, par2:val2......} ) will send/show/render x.ejs page to the client and pass an object of key-value pairs which will be displayed in x.ejs page.

RandomUserWithInternet commented 2 years ago

I haven't fixed synchronization yet, working on it now. Just checking to see if I have it right so far.

app.js:

function getFullNames(){
    let connectionFullName = {};
    for (let i in user.connections){
        var username = user.connections[i];
        var fullName = User.findOne({
            username: username
        }, function(err, found) {
            if(found) {
            return [found.fname, found.lname]
            }
        });

        if (fullName[0] == null){
            fullName[0] = "NA";
        }
        if (fullName[1] == null){
            fullname[1] = "NA";
        }

        connectionFullName[user.connections[i]] = fullName;
    }

    res.render('UserHome', connectionFullName);
}

UserHome.ejs: image

image

RandomUserWithInternet commented 2 years ago

Hopefully that's a correct implementation of the promise...

function getFullNames(){
    let connectionFullNames = {};

    function getNamesFromDB() {
        return new Promise((resolve) => {
            for (let i in user.connections){
                var username = user.connections[i];
                var fullName = User.findOne({
                    username: username
                }, function(err, found) {
                    if(found) {
                    return [found.fname, found.lname]
                    }
                });

                if (fullName[0] == null){
                    fullName[0] = "NA";
                }
                if (fullName[1] == null){
                    fullname[1] = "NA";
                }

                connectionFullNames[user.connections[i]] = fullName;
            }
            resolve();
        });
    }
    getNamesFromDB().then(res.render('UserHome', connectionFullName))
}
akhilchoudhary2k commented 2 years ago

Run the code and check

RandomUserWithInternet commented 2 years ago

Is there a test account I can log in with? It crashed when I tried to register.

RandomUserWithInternet commented 2 years ago

found the secret key, but then it crashed again. not sure what the problem is, might just make a new issue

RandomUserWithInternet commented 2 years ago

The errors seem to do with promises, so not so sure anymore. Let me try to run the base version.

RandomUserWithInternet commented 2 years ago

Has the same error.

RandomUserWithInternet commented 2 years ago

I hope I'm not doxing myself. image

akhilchoudhary2k commented 2 years ago

Because you have not setup the database correctly. I told you to go through readme file and steps on how to run the code locally.

If you don't go through the steps then It would be very difficult for me to help everytime.

RandomUserWithInternet commented 2 years ago

Followed the README from the beginning. Had to install the plugins (node, npm, and mongoDB) too lol. I had problems with the mongo commands from the terminal, so I used the Compass GUI instead. I don't know how that would affect anything though... I understand you can't help help everyone debug everything, however, I don't think I'll be able to test run it on my own. If someone else could test run my code, maybe that could work. I'll just make a pull request and label it as a draft.