#not sure how to use await?

114 messages · Page 1 of 1 (latest)

waxen jewel
#

anyone know how i can use await? using mongooose atm.

#
const passport = require('passport'); 
const LocalStrategy = require('passport-local').Strategy; 
const UserModel = require('./User'); 
// maps the passport fields to the names of fields in database
const localOpt = { 
 usernameField : 'email', 
 passwordField : 'password' 
}; 
// define strategy for validating login
const strategy = new LocalStrategy(localOpt, async (email, 
password, done) => { 
 try { 
 // Find the user in the DB associated with this email 
 const userChosen = await UserModel.findOne({ email: email }); 
 
 if( !userChosen ){ 
 //If the user isn't found in the database, set flash message
 return done(null, false, { message : 'Email not found'}); 
 } 
 // Validate password and make sure it matches the bcrypt digest 
 //stored in the database. If they match, return a value of true.
 const validate = await userChosen.isValidPassword(password); 
 if( !validate ){ 
 return done(null, false, { message : 'Wrong Password'}); 
 } 
 // Send the user information to the next middleware
 return done(null, userChosen, { message : 'Logged in Successfully'}); 
 } 
 catch (error) { 
 return done(error); 
 } 
}); 
// for localLogin, use our strategy to handle User login
passport.use('localLogin', strategy); 
 
// by default, passport uses sessions to maintain login status ... 
// you have to determine what to save in session via serializeUser 
// and deserializeUser. In our case, we will save the email in the 
// session data
passport.serializeUser( (user, done) => done(null, user.email) ); 
 
passport.deserializeUser( (email, done) => { 
 UserModel.findOne({ email: email }, (err, user) => done(err, 
user) ); 
});
#

im pretty sure that the resource im using to code this is outdated

urban pagoda
#

you should look up the official docs for the version of mongoose that npm installed

waxen jewel
#

wdym

#

as in my book

#

or the version i have installed

urban pagoda
#

the version that you have installed

#

or install the version of mongoose from your book

waxen jewel
#

cause ik the version i have installed is the lastest version

#

but its outdated

urban pagoda
#

so i'm saying either look up the docs for the version you have installed, OR install the version the book was based on

waxen jewel
#

and i was asking i guess how i can refactor my code and use promises instead of callbacks and what the difference is in terms of syntax

urban pagoda
#

but if you follow some book/guide but install a different version of a library, you will be susceptible to these kinds of breaking changes

waxen jewel
#

i guess it matters about context

#

like here its erroring when i use the function findOne

urban pagoda
#

what's the method signature for the version of findOne you have installed?

waxen jewel
#

hmm idk im using a text editor

#

not an ide

waxen jewel
#

i see

urban pagoda
#

I'm guessing it now returns a promise, but knowing how to look up the official API docs of libraries that you depend on is a critical skill

waxen jewel
#

ya i guess i had trouble understandign promises

#

vs callbacks

urban pagoda
#

so you don't need to use await with promises

#

probably the easiest way to update your code is to call .then() and then pass your callback to that

#

(I'm assuming a <<Query>> is a promise)

#

So try:

passport.deserializeUser( (email, done) => { 
  UserModel.findOne({ email: email })
    .exec()
    .then((err, user) => done(err, user)),
});
#

or something like that

waxen jewel
#

ya its just a bit diffcult. since book doesnt explain anything thats happening with the code and excepts you to edit it and figure stuff out. its like using someone esles code that has more experience than you and your trying to learn off somoene elses code base

#

ill try

urban pagoda
#

but learning about promises syntax probably requires a whole other tutorial in itself. I'm not prepared to teach you about promises here 🙂

#

I generally find that code samples from books that use 3rd party libraries often struggle from this problem where if you try to run it, you either need to find out the old version of the library that was used when the book was written, or you'll need to adapt it to newer APIs

waxen jewel
#

ya and it becomes harder

urban pagoda
#

If I'm trying to learn how to use a library, I try to use code samples from the library itself, as this has the highest chance of being maintained

waxen jewel
#

beause your the one trying to learn

waxen jewel
#

your trying to follow a larger scale project

urban pagoda
waxen jewel
#

from a book and youd be bettrer off rewriting code

#

than following book

#

but teacher grades you based off the book

#

and lab itself

#

its like your learning with broken code

urban pagoda
#

I personally would prioritize developing a learning strategy/system that will last me my whole career, rather than optimizing for what the teacher is looking for

waxen jewel
#

i guess. but if teacher is grading you based on your lab and following steps and taking screenshots

#

it becomes double the work

#

of learning stuff twice almost

#

i got this

#

i think thats correct

#

idk didnt give me an output

#

of what is evne suppose to happen

urban pagoda
waxen jewel
#

ya i agree. but do you want to pass the class or do you want to spend time doing it your way and then still having to do the book

#

at same time

#

of coruse if it wasnt for teacher i would not be learning tihs way

urban pagoda
#

it sounds like you're developing bad habits

waxen jewel
#

i am.

#

but employeers want this piece of paper that says you went to school. but at same time teachers give you books with buggy outdated code

urban pagoda
#

but employeers want this piece of paper that says you went to school

do they really?

#

or do they want someone who can do the job?

waxen jewel
#

well not neccarily

#

but id like to think that the paper gets you a chance to show your skills

#

against thousands of applications that get reviewed

#

and filitered

#

like at ibm where my uncle works they only accept people with 4 years/bachleors degree

#

i dont think they would even look at or consider your application

waxen jewel
#

but i dont think thats the reality. especially when trying to get your first job. after that it doesnt matter i feel

#

depends on company too. at game companies they could care less i feel.

urban pagoda
waxen jewel
#

idk im just trying to survive the system

#

lol

#

forutenly for me i want to get into tech art

#

so i dont think a degree really matters that much anyways

#

but its nice to have something to fall back on

urban pagoda
waxen jewel
#

prob. i dont think there is a black and white

urban pagoda
#

and maybe you're better off prioritizing your own learning than grades

waxen jewel
#

well im done with school after this

#

so now i can go learn stuff i want to learn

urban pagoda
waxen jewel
#

today is last day of college

#

thanks

urban pagoda
#

oh nice

waxen jewel
#

now i can go sculpt shit. i wanted to be a character artist. but doing it on the side as of right now

#

i was thinkojng about going into tech art

#

since its a combination of both

urban pagoda
#

what is tech art? like doing 3d modeling?

waxen jewel
#

its the person in charge of the pipeline

#

or making sure stuff is smooth sailing

#

for artists in terms of shaders, tools, rigging etc

#

i want to work more on the shader side in engine

urban pagoda
#

which pipeline?

#

for games?

waxen jewel
#

for games or movies

urban pagoda
#

ahh, i see

waxen jewel
#

mostly games

urban pagoda
#

that's cool

waxen jewel
#

im not sure if movies have tech artists on same level as games

#

cause games are more technical

#

its a very high level job

#

or job where you need to know yoru shit

#

cause there is not much of them per a team

urban pagoda
#

right

waxen jewel
#

only like 2-3 prob

#

your the person people fall back on when stuff doesnt work in engine. they are artists but dont know enough about tech side. but the programmers or technical people dont know much about art side

#

so tech artists comes in and makes sure artists know what they can or cant do

#

since they understand both sides

#

but anyhow its a cool job

urban pagoda
#

yeah

#

definitely