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

Items duplicating when restarting server #51

Open
Zendrex opened this issue Oct 20, 2017 · 5 comments
Open

Items duplicating when restarting server #51

Zendrex opened this issue Oct 20, 2017 · 5 comments

Comments

@Zendrex
Copy link

Zendrex commented Oct 20, 2017

So for some reason, every time I re-run app.js, the list just duplicates its self and adds more to the document as shown here:
image

I am unsure if this is how it was intended, however clearly duplicates are not something we want around.

My code so far:

const express = require('express');
const handlebars = require('express-handlebars');
const path = require('path');
const mongoose = require('mongoose');

const Item = require('./models/item');
const app = express();
const hbs = handlebars.create();

mongoose.connect('mongodb://127.0.0.1:27017/guide', {useMongoClient: true});
mongoose.Promise = global.Promise;

app.engine('hbs', hbs.engine);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');

// Item Table?
const awpAsiimov = new Item({
    name: 'AWP | Asiimov (Field-Tested)',
    price: 27.75
});

const akRedline = new Item({
    name: 'AK-47 | Redline (Field-Tested)',
    price: 5.52
});


awpAsiimov.save((err, item) => {
    if (err) {
        console.log(err);
    } else {
        console.log(item);
    }
});

akRedline.save((err, item) => {
    if (err) {
        console.log(err);
    } else {
        console.log(item);
    }
});

app.get('/', (req, res) => {
	Item.find({}, (err, items) => {
		if (err) {
			console.log(err);
		}
		res.render('main', { items });
	});
});

app.listen(3037);

ignore the useMongClient and mongoose.promise, those were for getting rid of the errors.

@andrewda
Copy link
Owner

This part in your code:

// Item Table?
const awpAsiimov = new Item({
    name: 'AWP | Asiimov (Field-Tested)',
    price: 27.75
});

const akRedline = new Item({
    name: 'AK-47 | Redline (Field-Tested)',
    price: 5.52
});


awpAsiimov.save((err, item) => {
    if (err) {
        console.log(err);
    } else {
        console.log(item);
    }
});

akRedline.save((err, item) => {
    if (err) {
        console.log(err);
    } else {
        console.log(item);
    }
});

will create 2 new items (an AK and an AWP) every time the server starts up. If you want to clear these out, drop the Items collection and run your code once. Then you can remove those lines and restart the app as many times as you want.

@andrewda
Copy link
Owner

What I should do is add a _id field to those new Items so they don't duplicate after every restart. I'll rename this issue to that and keep it open.

@andrewda andrewda changed the title Stuck at chapter 4.4 and the tables. Items duplicating when restarting server Oct 20, 2017
@Zendrex
Copy link
Author

Zendrex commented Oct 20, 2017

So, either by using db.item.drop() or by adding a _id field?

// Item Table?
const awpAsiimov = new Item({
    _id: 1,
    name: 'AWP | Asiimov (Field-Tested)',
    price: 27.75
});

const akRedline = new Item({
    _id: 2,
    name: 'AK-47 | Redline (Field-Tested)',
    price: 5.52
});

@andrewda
Copy link
Owner

Yep. Though the _id field will need to be a MongoDB id if I remember correctly, which is ~20 chars or something. I'll look into this more in a bit.

@filipbarak
Copy link
Contributor

A good solution would be to do this:

const Item = require('./models/item');

const items = [{
  name: 'AWP | Asiimov (Field_tested)',
  price: 27.75
}, {
  name: 'AK-47 | Redline (Field-Tested)',
  price: 5.52
}];

Item.remove({}).then(() => {
  Item.insertMany(items);
});

By doing this you remove every item in the DB and then insert an array of the predefined items into it.

Hope it helps!

@andrewda Maybe this could go into Chapter 4.4 instead of what's already there? Or it might be confusing for people that are new to mongoose/MongoDB?

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

3 participants