播种器文件和猫鼬设置有什么问题,我只能保存我的播种机型号?

时间:2017-10-27 18:15:15

标签: mongodb mongoose mongoose-schema

这是我的播种文件



var Product = require('../models/product.js');
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/cart', { useMongoClient: true });

mongoose.Promise = global.Promise;
mongoose.connection.once('connected', function() {
    console.log("Database connected successfully")
});

var products = [

    new Product({

        title: 'SuperMan Toy',
        description: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quasi totam praesentium commodi! Minima fugiat error possimus at ea quaerat a sit repudiandae laudantium. Ducimus quibusdam, assumenda iusto, doloribus quod debitis.',
        price: 550,
        imgPath: '#'

    }),
    new Product({

        title: 'Batman Toy',
        description: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quasi totam praesentium commodi! Minima fugiat error possimus at ea quaerat a sit repudiandae laudantium. Ducimus quibusdam, assumenda iusto, doloribus quod debitis.',
        price: 450,
        imgPath: '#'

    }),

    new Product({

        title: 'Hulk Toy',
        description: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quasi totam praesentium commodi! Minima fugiat error possimus at ea quaerat a sit repudiandae laudantium. Ducimus quibusdam, assumenda iusto, doloribus quod debitis.',
        price: 650,
        imgPath: '#'

    }),
];

var done = 0;

for (i = 0; i < products.length; i++) {

    products[i].save(function(err) {
        if (err) {
            console.log('Not Saved')
        }
    });

    done++;

    if (done === products.length) {
        mongoose.disconnect();
    }
}
&#13;
&#13;
&#13;

您可以在https://github.com/oaulakh/nodejs_testing

查看整个模板

https://github.com/oaulakh/nodejs_testing/blob/master/seeder/productSeeder.js

我在这里做错了,如果是的话我怎么解决这个问题让我的mongo开始保存我的模特?

0 个答案:

没有答案