这是我的Schema,位于models / adder文件夹中:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var schema = new Schema({
title: { type: String, required: true },
image: { type: String, required: true },
url: { type: String, required: true }
});
module.exports = mongoose.model('Product', schema);
现在这是我的index.js,其中所有路线都是为get,post和delete制作的:
var express = require('express');
var router = express.Router();
var Product = require('../models/adder');
var mongoose = require('mongoose');
mongoose.Promise = global.Promise;
mongoose.createConnection('localhost:27017/replica');
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('partials/index', { title: 'Express' });
});
router.get('/adder', function(req, res, next) {
res.render('adder/adder')
})
router.post('/added', function(req, res, next) {
var products = new Product({
title: req.body.title,
image: req.body.image,
url: req.body.url
});
products.save(function(err, done) {
if (err) {
console.log(err);
} else {
console.log('that have been saved')
}
res.redirect('/');
});
});
module.exports = router;
现在每当我提交四个产品时,我输入了所有数据,我已经安慰了它,我能够与我的本地mongoDB服务器建立联系。但我无法使用保存功能。任何人都可以指导我如何保存产品中的数据。