我一直在处理下面的路由文件。但是,它本质上是一个照片应用程序,我可以将数据存储在MondoDB中。但是,当我运行下面的代码时,终端会继续说出意外的令牌。然后。我在这里错过了什么吗?
var express = require('express');
var router = express.Router();
var app = express();
var multer = require('multer');
var upload = multer({dest: 'public/images/'});
var Photo = require ('../models/travelModels');
router.get('/', (req, res, next)=>{
Photo.find({})
.then((photos) =>{
res.render('lists', {
list: photos
});
});
});
router.post('/', upload.single('image'), (req, res, next)=>{
var path = "/static/images" + req.file.filename;
var item = {
destination: req.body.destination,
description: req.body.description,
imageurl: path,
mimetype: req.file.mimetype,
filename: req.file.filename
}
// save photo in database
var entry = new Photo (item);
entry.save();
.then(()=>{
res.redirect('/mylist')
});
// or catch error
.catch((err)=>{
if (err){
console.log(err);
}
});
});
module.exports = router;
答案 0 :(得分:1)
您需要在then
之前和catch
var entry = new Photo (item);
entry.save()
.then(()=>{
res.redirect('/mylist')
})
.catch((err)=>{
if (err){
console.log(err);
}
});