Monday, 29 May 2017

Mongoose doesn't save seed

I'm new to express and I'm following a tutorial. I'm currently in the seeding part. My problem is when I run this command, node product-seeder.js in the terminal, the data is not created even the db. My db is not yet existing but I read that it will be created if the data is saved. First, i got the error of Mongoose mpromise is deprecated so I put the mongoose.Promise = global.Promise; whenever my file requires mongoose.

Here is my app.js:

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');

var index = require('./routes/index');

var app = express();

//connect to mongose
mongoose.Promise = global.Promise;
mongoose.connect('localhost:27017/shopping-cart');

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', index);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;

My Product Schema in models/product.js

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var schema = new Schema({
        imagePath: {type: String, required: true},
        title: {type: String, required: true},
        description: {type: String, required: true},
        price: {type: Number, required: true}
});


module.exports = mongoose.model('Product', schema);

and lastly, my Product seeder in seeder directory:

var Product = require('../models/product');
var mongoose = require('mongoose');
mongoose.Promise = global.Promise;

var products = [
        new Product({
                imagePath: 'http://www.laptopmag.com/images/uploads/4989/g/apple-macbook-pro-13-2016-nw-g02.jpg',
                title: 'MacBook Pro 2016',
                description: 'Should we really call this a pro?',
                price: 1299
        }),
        new Product({
                imagePath: 'https://ss7.vzw.com/is/image/VerizonWireless/pdp-feature4-d-iphone7-bn-v2?&scl=1&bgc=ffffff&scl=1&scl=2',
                title: 'iPhone 7 Plus',
                description: 'iPhone with no headphone jack. Lmao',
                price: 899
        }),
        new Product({
                imagePath: 'https://www.extremetech.com/wp-content/uploads/2014/02/nokia-x-range.jpg',
                title: 'Nokia X',
                description: 'Failed Nokia Normandy project',
                price: 99
        }),
        new Product({
                imagePath: 'http://cdn2.gsmarena.com/vv/pics/nokia/nokia-n9-all-1.jpg',
                title: 'Nokia N9',
                description: 'Dead on arrival Meego OS powered smartphone',
                price: 56
        }),
        new Product({
                imagePath: 'https://store.storeimages.cdn-apple.com/4974/as-images.apple.com/is/image/AppleInc/aos/published/images/m/ac/macbook/air/macbook-air-gallery2-2014?wid=1292&hei=766&fmt=jpeg&qlt=95&op_sharpen=0&resMode=bicub&op_usm=0.5,0.5,0,0&iccEmbed=0&layer=comp&.v=1476297407703',
                title: 'MacBook Air',
                description: 'Most portable notebook from Apple',
                price: 999
        })
];

var done = 0;

for(var i=0; i < products.length; i++) {
        var saved = products[i].save();

        saved.then(function(err, result) {
                if(err) {
                        console.log(err);
                }
                else {
                        done++;
                        if(done === products.length)
                                exit();
                }
        });
}

function exit() {
        mongoose.disconnect();
        console.log("Product seeding done!");
}

Please tell me what I am doing wrong



via Welp

No comments:

Post a Comment