我正在尝试使用nodejs猫鼬,但当我插入值时它不起作用。值无法插入猫鼬并显示在页面上。当我尝试插入单个模式时,但当我向其中添加另一个模式时同一系列将无法正常工作。请任何人帮助。
router.js
const express = require('express');
const router = express.Router();
const Category = require('../Models/pagesModel');
const SuccessStory = require('../Models/pagesModel');
router.get('/',async (req,res) => {
res.render('index');
});
//category
router.get('/category',async (req,res) => {
const categories = await Category . find();
res.render('category' , {
categories
});
});
router.post('/addCategory', async (req,res) => {
const categories = new Category({
category : req.body.category ,
status : req.body.status
});
await categories.save();
console.log('received');
res.redirect('/category');
});
//success story
router.get('/successStory',async (req,res) => {
const successStories = await SuccessStory.find();
res.render('successStory' , {
successStories
});
});
router.post('/addsuccessStory', async (req,res) => {
const successStories = new SuccessStory({
successStory : req.body.successStory ,
successStoryStatus : req.body.successStoryStatus
});
await successStories.save();
console.log('received');
res.redirect('/successStory');
});
pagesModel.js
const CategorySchema = new Schema({
category: {
type:String
},
status: {
type:String
},
image1:String,
image2:String,
image3:String,
added_date:{
type: Date,
default: Date.now
}
})
const SuccessStorySchema = new Schema({
testimonial: {
type:String
},
testimonialStatus: {
type:String
},
image1:String,
image2:String,
image3:String
})
module.exports = mongoose.model('Category', CategorySchema);
module.exports = mongoose.model('SuccessStory',SuccessStorySchema);
page.ejs
<% for(var i = 0; i < successStories. length; i++) { %>
<tr>
<td><%= i+1 %></td>
<td><%= successStories[i].successStory %></td>
<td><%= successStories[i].successStoryStatus %></td>
</tr>
<% } %>
答案 0 :(得分:0)
尝试如下图所示导出和导入。
const categorySchema = mongoose.model('Category', CategorySchema),
const successStorySchema = mongoose.model('SuccessStory', SuccessStorySchema)
module.exports = { Category: categorySchema, SuccessStory: successStorySchema }
然后导入
var { Category } = require("../Models/pagesModel");
var { SuccessStory } = require("../Models/pagesModel");
Category.findOne()
SuccessStory.findOne()