node-steam-guide icon indicating copy to clipboard operation
node-steam-guide copied to clipboard

Items duplicating when restarting server

Open Zendrex opened this issue 7 years ago • 5 comments

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.

Zendrex avatar Oct 20 '17 09:10 Zendrex

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 avatar Oct 20 '17 18:10 andrewda

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 avatar Oct 20 '17 18:10 andrewda

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
});

Zendrex avatar Oct 20 '17 18:10 Zendrex

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.

andrewda avatar Oct 20 '17 19:10 andrewda

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?

filipbarak avatar Feb 10 '18 21:02 filipbarak