Open akhilchoudhary2k opened 2 years ago
Could I take this issue?
Yes pls go ahead @RandomUserWithInternet , but try to raise PR ASAP other wise I may reassign it. As it is an easy fix.
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.
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.
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.
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.
@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
}
});
Yeah I'll try it out.
app.js UserHome.ejs Here's what I did... Not sure if I implemented it correctly.
you are on the right lines, but it's not correct wrt the implementatin will help you fix it tomorrow.
Thanks! I appreciate you helping me out, in spite of my incompetence.
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?
@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
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.
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.
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:
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))
}
Run the code and check
Is there a test account I can log in with? It crashed when I tried to register.
found the secret key, but then it crashed again. not sure what the problem is, might just make a new issue
The errors seem to do with promises, so not so sure anymore. Let me try to run the base version.
Has the same error.
I hope I'm not doxing myself.
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.
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.
Note: Show
NA
if the value is unavailablesee image for reference.