mosh-hamedani / vidly-api-node

307 stars 281 forks source link

seed.js does not populate users #10

Open cyberena opened 5 years ago

cyberena commented 5 years ago

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":"tom@tom.com", "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 commented 5 years ago

I have posted the work around... let me know if you run into any issues

On Tue, Mar 19, 2019 at 7:05 PM cozyxin notifications@github.com wrote:

I also have this problem and I cannot run node index.js to start the server.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/mosh-hamedani/vidly-api-node/issues/10#issuecomment-474654970, or mute the thread https://github.com/notifications/unsubscribe-auth/AADW_5gaa57ku2Qkwat54KPTMXp6bCiMks5vYZeAgaJpZM4b3onW .

-- Best regards, Philip

cyberena commented 5 years ago

I have posted a workaround here which allows you to finish, hope this helps! https://github.com/mosh-hamedani/vidly-api-node/issues/10

On Wed, Apr 10, 2019 at 7:26 AM Saad Tarek notifications@github.com wrote:

@mosh-hamedani https://github.com/mosh-hamedani any help!

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/mosh-hamedani/vidly-api-node/issues/10#issuecomment-481713391, or mute the thread https://github.com/notifications/unsubscribe-auth/AADW__QXAML4VXSblZGQiyNYRa_JCx6Xks5vffR4gaJpZM4b3onW .

-- Best regards, Philip

AronBe commented 5 years ago

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 commented 5 years ago

This is really helpful, thanks

vfweber commented 4 years ago

Issue still there. Thank you for the work around!

Turkinolith commented 4 years ago

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 commented 4 years ago

@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 commented 4 years ago

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 commented 4 years ago

@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 commented 4 years ago

@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 commented 4 years ago

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 commented 4 years ago

@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 commented 4 years ago

I was able to get things working with bcrypt ^4.0.1

veronicapc92 commented 4 years ago

@iamraghudb I used bcryptjs ^2.4.3

iamraghudb commented 4 years ago

Thank You Veronica @veronicapc92 and I have another error In chapter 11 from Authentication and Authorization after installing jwt-decode@2.2.0 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 commented 4 years ago

@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 commented 4 years ago

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: "seed@email.com", 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 commented 2 years ago

@cyberena Thanks a lot. You made my day !

Mohamedaly77 commented 2 years ago

@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 commented 1 year ago

Hello dude do you still need help?

7 Kasım 2022 Pazartesi tarihinde Mohamed Aly @.***> yazdı:

@cyberena https://github.com/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

— Reply to this email directly, view it on GitHub https://github.com/mosh-hamedani/vidly-api-node/issues/10#issuecomment-1305752524, or unsubscribe https://github.com/notifications/unsubscribe-auth/AWZOYEHGHOSBATX34BMUHLLWHELOJANCNFSM4G66RHLA . You are receiving this because you commented.Message ID: @.***>

Mohamedaly77 commented 1 year ago

Hello

No i actually figured it out , thanks for reply

On Sun, 27 Nov 2022 at 7:04 PM AltairDaMasyaf @.***> wrote:

Hello dude do you still need help?

7 Kasım 2022 Pazartesi tarihinde Mohamed Aly @.***> yazdı:

@cyberena https://github.com/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

— Reply to this email directly, view it on GitHub < https://github.com/mosh-hamedani/vidly-api-node/issues/10#issuecomment-1305752524 , or unsubscribe < https://github.com/notifications/unsubscribe-auth/AWZOYEHGHOSBATX34BMUHLLWHELOJANCNFSM4G66RHLA

. You are receiving this because you commented.Message ID: @.***>

— Reply to this email directly, view it on GitHub https://github.com/mosh-hamedani/vidly-api-node/issues/10#issuecomment-1328295589, or unsubscribe https://github.com/notifications/unsubscribe-auth/AYL6N2LS5ECBES7F2E6L5XLWKOIAPANCNFSM4G66RHLA . You are receiving this because you commented.Message ID: @.***>

AltairDaMasyaf commented 1 year ago

Ur welcome sorry for late replay:) if you need any help you can always ask. And if I don't know it at least will learn it 👍

27 Kasım 2022 Pazar tarihinde Mohamed Aly @.***> yazdı:

Hello

No i actually figured it out , thanks for reply

On Sun, 27 Nov 2022 at 7:04 PM AltairDaMasyaf @.***> wrote:

Hello dude do you still need help?

7 Kasım 2022 Pazartesi tarihinde Mohamed Aly @.***> yazdı:

@cyberena https://github.com/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

— Reply to this email directly, view it on GitHub < https://github.com/mosh-hamedani/vidly-api-node/issues/10#issuecomment- 1305752524 , or unsubscribe < https://github.com/notifications/unsubscribe-auth/ AWZOYEHGHOSBATX34BMUHLLWHELOJANCNFSM4G66RHLA

. You are receiving this because you commented.Message ID: @.***>

— Reply to this email directly, view it on GitHub https://github.com/mosh-hamedani/vidly-api-node/issues/10#issuecomment- 1328295589, or unsubscribe https://github.com/notifications/unsubscribe-auth/ AYL6N2LS5ECBES7F2E6L5XLWKOIAPANCNFSM4G66RHLA . You are receiving this because you commented.Message ID: @.***>

— Reply to this email directly, view it on GitHub https://github.com/mosh-hamedani/vidly-api-node/issues/10#issuecomment-1328303018, or unsubscribe https://github.com/notifications/unsubscribe-auth/AWZOYEGFTN27I3ADPN7TRV3WKOMSLANCNFSM4G66RHLA . You are receiving this because you commented.Message ID: @.***>

svmed2050 commented 1 year ago

@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 commented 1 year ago

@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!