jameshibbard / demo-node-app

Build a beginner app with Node, Bootstrap & MongoDB
32 stars 39 forks source link

Unable to save data to MongoDB database #2

Closed HalfMillennium closed 4 years ago

HalfMillennium commented 4 years ago

For some reason, I haven't been able to save any data to my MongoDB data base. I first discovered this issue on my own version of your code, but then I cloned this exact repository and followed all of your installation instructions - the same deal.

I don't get any errors, and the .then() method executes and behaves as if registration was successful. Everything works properly except the actual persisting of the data. I checked my user permissions on MongoDB's website, and it says that they're able to read/write to any database.

jameshibbard commented 4 years ago

Hi,

Sorry to hear that you're having problems.

I just cloned and ran this repo to my system (Linux Mint 19.2, Mode 10.17.0, Mongo 3.6.3) and everything works as expected. This means a little more trouble shooting is required.

What is your OS / Node version / Mongo version?

Open a Mongo shell (by typing mongo from the command prompt) and enter:

use test
db.test.insert({ 'name': 'jim' })
db.getCollection('test').find()

What does that output?

HalfMillennium commented 4 years ago

I'm on Windows 10, Node version 12.14.0 and Mongo version 4.2.2. The output of running those commands is: { "_id" : ObjectId("5e0b7e3246a7f704d56c067e"), "name" : "jim" }

jameshibbard commented 4 years ago

Ok, well you can obviously write to your MongoDB, so that's not the problem. Can you post the contents of the .env file in your root directory (taking care to obscure any passwords).

HalfMillennium commented 4 years ago

DATABASE=mongodb://localhost:27017/sp-node-article

jameshibbard commented 4 years ago

Ok. I'll see if I can recreate your environment later on and test things out. In the meantime can you confirm the steps to reproduce your problem.

Did I get that right?

HalfMillennium commented 4 years ago

Yes, that's right.

jameshibbard commented 4 years ago

Hi,

So I installed a copy of Win 10 on Virtual Box, installed Node v12.14.0 and Mongo v4.2.2.

I then did the following:

  1. Download the repo as a zip file and extracted it
  2. Changed into the project root and ran npm install
  3. Created a .env file in the project root
  4. Added the following to the .env file: DATABASE=mongodb://localhost:27017/node-demo-application
  5. Ran npm run watch
  6. Visited http://localhost:3000 and created a new registration
  7. Visited http://localhost:3000/registrations where I could view the successful registration

I'm afraid to say that the data was persisted as expected, meaning that it's something else with your setup which is causing the problem.

If I was you, I would try stripping out parts of the program until you can localize exactly where it is failing. When you have done this, feel free to head over to SitePoint forums and ask for help there. as described in the Basic Setup section of the article.

For now, as I can't reproduce this error, I'm closing the issue.

Sorry I couldn't help more :(

HalfMillennium commented 4 years ago

Hm. Well, I'm incredibly grateful for your efforts, you've definitely set me on the right track.

HalfMillennium commented 4 years ago

Actually, I made a mistake. The result of testing the database in command line is actually "uncaught exception: ReferenceError: node is not defined".

On Wed, Jan 1, 2020 at 11:29 AM James Hibbard notifications@github.com wrote:

Closed #2 https://github.com/jameshibbard/demo-node-app/issues/2.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/jameshibbard/demo-node-app/issues/2?email_source=notifications&email_token=ABI6V5KLLVBDG4VFTKPPOGDQ3TAHTA5CNFSM4KBP2HV2YY3PNVWWK3TUL52HS4DFWZEXG43VMVCXMZLOORHG65DJMZUWGYLUNFXW5KTDN5WW2ZLOORPWSZGOVXL4KVQ#event-2916599126, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABI6V5KP5YMME6QCDXKXMR3Q3TAHTANCNFSM4KBP2HVQ .

jameshibbard commented 4 years ago

Oh ok, so when you run:

use test
db.test.insert({ 'name': 'jim' })
db.getCollection('test').find()

The result is uncaught exception: ReferenceError: node is not defined and not { "_id" : ObjectId("5e0b7e3246a7f704d56c067e"), "name" : "jim" } as previously stated.

Did I get that right?

HalfMillennium commented 4 years ago

Yes, I didn't realize that I had slightly misspelled the name of my database.

On Thu, Jan 2, 2020, 2:24 AM James Hibbard notifications@github.com wrote:

Oh ok, so when you run:

use test db.test.insert({ 'name': 'jim' }) db.getCollection('test').find()

The result is uncaught exception: ReferenceError: node is not defined and not { "_id" : ObjectId("5e0b7e3246a7f704d56c067e"), "name" : "jim" } as previously stated.

Did I get that right?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/jameshibbard/demo-node-app/issues/2?email_source=notifications&email_token=ABI6V5O2SDO7UTR27O55KILQ3WJCFA5CNFSM4KBP2HV2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEH5Y54A#issuecomment-570134256, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABI6V5M33KNXDYAOUYOAGNLQ3WJCFANCNFSM4KBP2HVQ .

HalfMillennium commented 4 years ago

Unless those commands were meant to be entered verbatim? When I run that exactly, I get a successful response, but I'm assuming I'm meant to enter my own database name in place of "test"?

On Thu, Jan 2, 2020, 4:30 AM Garrett Chestnut glc2266@gmail.com wrote:

Yes, I didn't realize that I had slightly misspelled the name of my database.

On Thu, Jan 2, 2020, 2:24 AM James Hibbard notifications@github.com wrote:

Oh ok, so when you run:

use test db.test.insert({ 'name': 'jim' }) db.getCollection('test').find()

The result is uncaught exception: ReferenceError: node is not defined and not { "_id" : ObjectId("5e0b7e3246a7f704d56c067e"), "name" : "jim" } as previously stated.

Did I get that right?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/jameshibbard/demo-node-app/issues/2?email_source=notifications&email_token=ABI6V5O2SDO7UTR27O55KILQ3WJCFA5CNFSM4KBP2HV2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEH5Y54A#issuecomment-570134256, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABI6V5M33KNXDYAOUYOAGNLQ3WJCFANCNFSM4KBP2HVQ .

jameshibbard commented 4 years ago

Nope. Should work with test.

What were the commands you are running that gave you the uncaught exception: ReferenceError: node is not defined response?

HalfMillennium commented 4 years ago

I typed in the name of my database

On Thu, Jan 2, 2020, 4:36 AM James Hibbard notifications@github.com wrote:

Nope. Should work with test.

What were the commands you are running that gave you the uncaught exception: ReferenceError: node is not defined response?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/jameshibbard/demo-node-app/issues/2?email_source=notifications&email_token=ABI6V5K76NH3QSHY5HB2OKLQ3WYTXA5CNFSM4KBP2HV2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEH56NYQ#issuecomment-570156770, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABI6V5OZVSK2RFIWDB66P6TQ3WYTXANCNFSM4KBP2HVQ .

jameshibbard commented 4 years ago

So, using test all is good, but using sp-node-article you get an error?

use test
db.test.insert({ 'name': 'jim' })
db.getCollection('test').find()

> { "_id" : ObjectId("5e0b7e3246a7f704d56c067e"), "name" : "jim" }

But:

use sp-node-article

> uncaught exception: ReferenceError: node is not defined

Did I get that right?

HalfMillennium commented 4 years ago

Well, no, it lets me 'use' the database, but when I try to execute 'db.sp-node-article.insert(...)', I get that error.

On Thu, Jan 2, 2020 at 3:13 PM James Hibbard notifications@github.com wrote:

So, using test all is good, but using sp-node-article you get an error?

use test db.test.insert({ 'name': 'jim' }) db.getCollection('test').find()

{ "_id" : ObjectId("5e0b7e3246a7f704d56c067e"), "name" : "jim" }

But:

use sp-node-article

uncaught exception: ReferenceError: node is not defined

Did I get that right?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/jameshibbard/demo-node-app/issues/2?email_source=notifications&email_token=ABI6V5K5RS3MQN5P7KOOZM3Q3ZDG7A5CNFSM4KBP2HV2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEH7ITXA#issuecomment-570329564, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABI6V5MHRWFIRNRVXPE5QKLQ3ZDG7ANCNFSM4KBP2HVQ .

jameshibbard commented 4 years ago

Ah right, that's not an error so much as the mongo shell not accepting the name of the collection which contains a hyphen.

This should work:

use sp-node-article
db['sp-node-article'].insert({ 'name': 'jim' })
db.getCollection('sp-node-article').find()

Which unfortunately still doesn't explain why you cannot insert any data into the data base using the app.

HalfMillennium commented 4 years ago

Oh, okay. Well, strangely, it says the insert into sp-node-article is successful, but I get the same issue - nothing in the database. I even retrieved the data, but still nothing shows up in Compass.

On Fri, Jan 3, 2020, 2:03 AM James Hibbard notifications@github.com wrote:

Ah right, that's not an error so much as the mongo shell not accepting the name of the collection which contains a hyphen.

This should work:

use sp-node-article db['sp-node-article'].insert({ 'name': 'jim' }) db.getCollection('sp-node-article').find()

Which unfortunately doesn't explain why you cannot insert any data into the data base using the app.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/jameshibbard/demo-node-app/issues/2?email_source=notifications&email_token=ABI6V5PPZ2HCCAQ62MD5BMDQ33PLVA5CNFSM4KBP2HV2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEIAOIDA#issuecomment-570483724, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABI6V5KIQUCYDJEXK46GJW3Q33PLVANCNFSM4KBP2HVQ .

jameshibbard commented 4 years ago

Man, that makes zero sense. So you can insert and retrieve data from the mongo shell, but nothing is showing up in Compass? I would almost be tempted to say that the issue is with Compass, but you said you are having the same problem with the app, which is independent of Compass.

HalfMillennium commented 4 years ago

Exactly. This is all incredibly confusing.

On Fri, Jan 3, 2020, 10:37 AM James Hibbard notifications@github.com wrote:

Man, that makes zero sense. So you can insert and retrieve data from the mongo shell, but nothing is showing up in Compass? I would almost be tempted to say that the issue is with Compass, but you said you are having the same problem with the app, which is independent of Compass.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/jameshibbard/demo-node-app/issues/2?email_source=notifications&email_token=ABI6V5IMCA7YBLQ4YWXHQDTQ35LUBA5CNFSM4KBP2HV2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEIBMNUY#issuecomment-570607315, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABI6V5OSHA5L745W3TEKD43Q35LUBANCNFSM4KBP2HVQ .

HalfMillennium commented 4 years ago

Alright, one last thing, as I understand that this issue might just be beyond what you can assist me with.

But I've just tried it on my Mac, and I have mostly the same issue - except localhost:3000/registrations DOES show all successful registrations, like it should. Does this mean that the database I've been viewing on Compass isn't the same one connected to the code?

On Fri, Jan 3, 2020, 10:40 AM Garrett Chestnut glc2266@gmail.com wrote:

Exactly. This is all incredibly confusing.

On Fri, Jan 3, 2020, 10:37 AM James Hibbard notifications@github.com wrote:

Man, that makes zero sense. So you can insert and retrieve data from the mongo shell, but nothing is showing up in Compass? I would almost be tempted to say that the issue is with Compass, but you said you are having the same problem with the app, which is independent of Compass.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/jameshibbard/demo-node-app/issues/2?email_source=notifications&email_token=ABI6V5IMCA7YBLQ4YWXHQDTQ35LUBA5CNFSM4KBP2HV2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEIBMNUY#issuecomment-570607315, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABI6V5OSHA5L745W3TEKD43Q35LUBANCNFSM4KBP2HVQ .

jameshibbard commented 4 years ago

Does this mean that the database I've been viewing on Compass isn't the same one connected to the code?

I guess so. Which DB have you been viewing?

HalfMillennium commented 4 years ago

Or maybe a different cluster? Because I check every database in my cluster whenever I test the app.

On Mon, Jan 6, 2020, 3:43 AM James Hibbard notifications@github.com wrote:

Does this mean that the database I've been viewing on Compass isn't the same one connected to the code?

I guess so. Which DB have you been viewing?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/jameshibbard/demo-node-app/issues/2?email_source=notifications&email_token=ABI6V5JNINTZHAAKQD4C2ITQ4LVJXA5CNFSM4KBP2HV2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEIEZSHQ#issuecomment-571054366, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABI6V5P2SHEH4DXKQOXEAILQ4LVJXANCNFSM4KBP2HVQ .

tganyan commented 4 years ago

A bit late and you probably already figured this out, but I had the exact same issue just now and finally figured out it was my own ignorance with compass. Just refreshing the collection doesn't seem to work, you also need to hit the little refresh button in the top left (where it lists how many dbs and how many collections you have total) to do a full refresh of the db list. After doing that once, I can refresh the collection and see new entries. Your issue might go deeper than that, but in case it derailed you and you gave up on it, figured I'd drop a comment.

OS: Windows 10