Mongoose不保存数据

时间:2017-05-29 15:43:56

标签: javascript node.js mongodb express mongoose

我是新来的,我正在关注一个教程。我现在正在播种部分。我的问题是当我在终端中运行此命令node product-seeder.js时,甚至不会创建数据。我的数据库尚不存在,但我读到如果保存数据将会创建它。首先,我收到了Mongoose mpromise is deprecated的错误,因此每当我的文件需要mongoose.Promise = global.Promise;时,我都会设置mongoose

这是我的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;

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

最后,我在seeder目录中的产品播种机:

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

请告诉我我做错了什么

1 个答案:

答案 0 :(得分:0)

您在product-seeder.js文件中缺少通过mongoose与mongodb的连接

var Product = require('../models/product');
var mongoose = require('mongoose');
mongoose.Promise = global.Promise;
//add this
mongoose.connect('localhost:27017/shopping-cart');

添加上述连接语句应该可以解决问题。