Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

seed.js does not populate users #10

Open
cyberena opened this issue Mar 16, 2019 · 24 comments
Open

seed.js does not populate users #10

cyberena opened this issue Mar 16, 2019 · 24 comments

Comments

@cyberena
Copy link

Hi, this problem still exists. It seems the github download as of today is incomplete and missing parts which completely hinders the ability to continue the tutorial. I spent a while to figure it out and this is how you can insert it yourself and then also change the api so that it is readable by the web.

Go to command prompt and type "mongo" to enter the shell
type "use vidly" to use that db
type
db.users.insert(
{
"name":"Tom Hanks",
"email":"[email protected]",
"password":"234242"
})
Now youve inserted and created a new collections called "users" with one object

Go to vidly-api-node folder and open routes/users.js file
insert this code to top after all the const declarations

router.get("/", async (req, res) => {
const users = await User.find();
res.send(users);
});

restart node using the "node index.js" command

use postman and do a test call to
http://localhost:3900/api/users

It works.

Dear Mosh, your tutorials are awesome, can you please fix this so people can finish them and recommend you even more around the world. Thank you.

@cyberena cyberena changed the title seed.js does not popular users seed.js does not populate users Mar 16, 2019
@cyberena
Copy link
Author

cyberena commented Mar 20, 2019 via email

@cyberena
Copy link
Author

cyberena commented Apr 10, 2019 via email

@AronBe
Copy link

AronBe commented Apr 12, 2019

thank you for helping to resolve this!

just one thing to add for newbies, start mongo by running "mongod" in one command prompt before you run "mongo" in another

@vedant0712
Copy link

This is really helpful, thanks

@vfweber
Copy link

vfweber commented Dec 30, 2019

Issue still there. Thank you for the work around!

@Turkinolith
Copy link

Ran into this last night too. Thanks for the work around! I'm surprised that it's been 8 months and the issue still hasn't been addressed.

@ybakhshi
Copy link

ybakhshi commented Feb 2, 2020

@cyberena in section Authentication and Authorization, i get this error in console:
POST http://localhost:3900/api/users 400 (Bad Request)
createError.js:16 Uncaught (in promise) Error: Request failed with status code 400
at createError (createError.js:16)
at settle (settle.js:18)
at XMLHttpRequest.handleLoad (xhr.js:59)

do you know any work around for this. The code is the same as mosh did. I also can not insert data via postman and receive 500 internal sever error.
Your help in this regard will be highly appreciated!

@veronicapc92
Copy link

veronicapc92 commented Mar 31, 2020

thanks for the help @cyberena !

I have a similar issue to the one @ybakhshi stated above.

I get the same 500 internal server error and when trying to submit the registration form I get "error: undefined No callback function was given" in Git Bash where I started the server.

Could this be related to the way we have created users? Anyone that can help with this please?

Many thanks in advance

@ybakhshi
Copy link

@veronicapc92 this is so annoying and no one really helped me out! i started another course in Udemy and progressed well . although i learned a lot here too. Are you also a new learner?

@veronicapc92
Copy link

veronicapc92 commented Mar 31, 2020

@ybakhshi it's quite frustrating sometimes 😅 yes I'm new to web development, I started coding like 3 months ago

I think I found the solution. The problem is coming from bcrypt. Bcrypt-nodejs is now deprecated so I have installed bcryptjs instead.

Then I went to vidly-api-node/routes/users and replaced this code:

const salt = await bcrypt.genSalt(10); user.password = await bcrypt.hash(user.password, salt, null); await user.save();

with this code:

const saltRounds = 10; bcrypt.hash(user.password, saltRounds, async function(err, hash) { if (err) throw err; user.password = hash; await user.save(); });

Not sure the code is 100% correct but it seems to work. Please let me know if this works for you or if you find a better way of doing it

@ybakhshi
Copy link

ybakhshi commented Apr 6, 2020

hey @veronicapc92 ! i resolved that bug in the same way i guess . it was two months ago and i don't remember much. Perhaps we can do co-learning and help fixing our issues. You find me here on linkedin :
https://www.linkedin.com/in/yonus-bakhshi-271a48116/

@iamraghudb
Copy link

@ybakhshi it's quite frustrating sometimes 😅 yes I'm new to web development, I started coding like 3 months ago

I think I found the solution. The problem is coming from bcrypt. Bcrypt-nodejs is now deprecated so I have installed bcryptjs instead.

Then I went to vidly-api-node/routes/users and replaced this code:

const salt = await bcrypt.genSalt(10); user.password = await bcrypt.hash(user.password, salt, null); await user.save();

with this code:

const saltRounds = 10; bcrypt.hash(user.password, saltRounds, async function(err, hash) { if (err) throw err; user.password = hash; await user.save(); });

Not sure the code is 100% correct but it seems to work. Please let me know if this works for you or if you find a better way of doing it

Which bcryptjs version did you install??

@Turkinolith
Copy link

I was able to get things working with bcrypt ^4.0.1

@veronicapc92
Copy link

@iamraghudb I used bcryptjs ^2.4.3

@iamraghudb
Copy link

iamraghudb commented Apr 8, 2020

Thank You Veronica @veronicapc92
and I have another error
In chapter 11 from Authentication and Authorization after installing [email protected] I am getting
InvalidTokenError: Invalid token specified: Cannot read property 'replace' of undefined this error.Can you please help me with this.Thanks in advance

@veronicapc92
Copy link

@iamraghudb unfortunately I don't know what can be causing the error without seeing your code. However I recently discovered that Mosh has a forum where we can ask our questions https://forum.codewithmosh.com/

I hope it helps!

@ondrovic
Copy link

ondrovic commented Jul 2, 2020

I managed to get this working directly from the seed.js. You will need to make the following changes

Top near the other const's
const { User } = require("./models/user");

Before the seed()
const dataUsers = [ { name: "Seed User", email: "[email protected]", password: "password", }, ];

Inside seed() before 1st for
await User.deleteMany({});

Inside seed() before mongoose.disconnect()
for (let user of dataUsers) { await new User({ name: user.name, email: user.email, password: user.password, }).save(); }

No more manual creating from the 1st step

@AltairDaMasyaf
Copy link

@cyberena
Thanks a lot. You made my day !

@Mohamedaly77
Copy link

@cyberena

Thanks a lot. You made my day !

Hello :)

Since you are the last one that solved the problem could you please share your code how you solved this issue?

Thanks

@AltairDaMasyaf
Copy link

AltairDaMasyaf commented Nov 27, 2022 via email

@Mohamedaly77
Copy link

Mohamedaly77 commented Nov 27, 2022 via email

@AltairDaMasyaf
Copy link

AltairDaMasyaf commented Nov 28, 2022 via email

@svmed2050
Copy link

@ybakhshi it's quite frustrating sometimes 😅 yes I'm new to web development, I started coding like 3 months ago

I think I found the solution. The problem is coming from bcrypt. Bcrypt-nodejs is now deprecated so I have installed bcryptjs instead.

Then I went to vidly-api-node/routes/users and replaced this code:

const salt = await bcrypt.genSalt(10); user.password = await bcrypt.hash(user.password, salt, null); await user.save();

with this code:

const saltRounds = 10; bcrypt.hash(user.password, saltRounds, async function(err, hash) { if (err) throw err; user.password = hash; await user.save(); });

Not sure the code is 100% correct but it seems to work. Please let me know if this works for you or if you find a better way of doing it

Thank you. It was very helpful and solved the issue ))

@Valentin715
Copy link

@ybakhshi it's quite frustrating sometimes 😅 yes I'm new to web development, I started coding like 3 months ago

I think I found the solution. The problem is coming from bcrypt. Bcrypt-nodejs is now deprecated so I have installed bcryptjs instead.

Then I went to vidly-api-node/routes/users and replaced this code:

const salt = await bcrypt.genSalt(10); user.password = await bcrypt.hash(user.password, salt, null); await user.save();

with this code:

const saltRounds = 10; bcrypt.hash(user.password, saltRounds, async function(err, hash) { if (err) throw err; user.password = hash; await user.save(); });

Not sure the code is 100% correct but it seems to work. Please let me know if this works for you or if you find a better way of doing it

Thank you! This helped today!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests