为什么find({})返回undefined?



嗨,我正在制作一个基本的社交应用程序,目前有一个Account和一个Post模型。

我能够显示Account模型加上更多,但是当我去显示所有帖子时,我特别遇到Post模型的问题。

猫鼬函数Post.find({}).exec(function(err, posts)allPosts.js保持返回undefined,即使我在数据库中有帖子。

注意:我可以成功地发布到posts集合,只是不能从他们读取。

我可以显示所有帐户,但不能显示所有帖子。


更新:我现在可以创建一个帖子,然后被重定向回allPosts.pug页面,看到除了刚刚创建的帖子之外的所有帖子,看看addNewPost.js

我希望能够从我的登录页面,并有所有的帖子显示检查出index.js

/* Account.js */
const mongoose = require("mongoose")
const Account = mongoose.model("Account", {
name: String,
lastName: String,
email: {type: String, unique: true, lowercase: true, required: true},
phoneNumber: String,
password: String
})
module.exports = Account
/* Post.js */
const mongoose = require("mongoose")
const Post = mongoose.model("Post", {
emailId: {type: String, required: true},
postTitle: {type: String, required: true},
postMessage: {type: String, required: true},
datePosted: {type: Date, default: Date.now},
})
module.exports = Post
/* allPosts.js */
var express = require('express');
var router = express.Router();
const Account = require("../models/account")
const Post = require("../models/post")
/* GET all posts page. */
router.get('/', function(req, res, next) {
Post.find({}).exec(function(err, posts) {
res.render("allPosts", {title: "Social Application", posts: posts})
})
})
module.exports = router;
/* allPosts.pug */
extends layout 
block content 
h1 #{title}
if posts === undefined
h1 No posts to show here 
else 
each post in posts
div(class="container")
div(class="jumbotron")
h1= post.postTitle 
h1= post.postMessage
br

a(href="/addNewPost"): button(class="btn btn-primary float-end") Add post
a(href="/logout"): button(class="btn btn-primary float-end") Logout
/* addNewPost.js */
var express = require('express');
const Post = require('../models/post');
var router = express.Router();
/* GET all posts page. */
router.get('/', function(req, res, next) {
res.render("addNewPost", {title: "Social Application"})
});
router.post('/', function(req, res, next) {
var emailId = req.session.email
var postTitle = req.body.postTitle
var postMessage = req.body.postMessage
var postData = {
emailId: emailId,
postTitle: postTitle,
postMessage: postMessage
}
var aPost = new Post(postData)
aPost.save(function(err) {
if (err) {
console.log(err)
} else {
console.log("Post saved successfully !")
}
})
Post.find({}).exec(function(err, posts) {
res.render("allPosts", {title: "All posts", posts: posts})
})
})
module.exports = router;
/* index.js */
var express = require('express');
var router = express.Router();
const Account = require("../models/account")
const bcrypt = require("bcrypt");
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: "Social Application" });
});
/* POST home page */
router.post('/', function(req, res, next) {
var email = req.body.email
var password = req.body.password
Account.findOne({email:email}).exec(function(err, account) {
if (account) {
if (bcrypt.compareSync(password, account.password)) {
req.session.email = account.email
req.session.userLoggedIn = true
res.render("allPosts", {title: "All Posts"})
} else {
res.render("index", {error: "Your credentials are incorrect !", title: "Social Application"})
}
} else {
res.render("index", {error: "This account does not exist !", title: "Social Application"})
}
})
})
module.exports = router;
/* Trace */
TypeError: C:UsersbmxfiDesktopcodeNodeJavascriptSocialApplicationSocialApplicationviewsallPosts.pug:7
5| 
6|     
> 7|     each post in posts
8|         div(class="container")
9|             div(class="jumbotron")
10|                 h1= post.postTitle 
Cannot read property 'length' of undefined
at eval (eval at wrap (C:Usersbmxfinode_modulespugnode_modulespug-runtimewrap.js:6:10), <anonymous>:36:32)
at eval (eval at wrap (C:Usersbmxfinode_modulespugnode_modulespug-runtimewrap.js:6:10), <anonymous>:71:4)
at template (eval at wrap (C:Usersbmxfinode_modulespugnode_modulespug-runtimewrap.js:6:10), <anonymous>:93:7)
at Object.exports.renderFile (C:Usersbmxfinode_modulespuglibindex.js:454:38)
at Object.exports.renderFile (C:Usersbmxfinode_modulespuglibindex.js:444:21)
at View.exports.__express [as engine] (C:Usersbmxfinode_modulespuglibindex.js:493:11)
at View.render (C:Usersbmxfinode_modulesexpresslibview.js:135:8)
at tryRender (C:Usersbmxfinode_modulesexpresslibapplication.js:640:10)
at Function.render (C:Usersbmxfinode_modulesexpresslibapplication.js:592:3)
at ServerResponse.render (C:Usersbmxfinode_modulesexpresslibresponse.js:1012:7)

修复如下,只需将redirect("allPosts")替换为res.render("allPosts", {title: "Social Application")

/* index.js */
var express = require('express');
var router = express.Router();
const Account = require("../models/account")
const bcrypt = require("bcrypt");
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: "Social Application" });
});
/* POST home page */
router.post('/', function(req, res, next) {
var email = req.body.email
var password = req.body.password
Account.findOne({email:email}).exec(function(err, account) {
if (account) {
if (bcrypt.compareSync(password, account.password)) {
req.session.email = account.email
req.session.userLoggedIn = true
res.redirect("allPosts")
} else {
res.render("index", {error: "Your credentials are incorrect !", title: "Social Application"})
}
} else {
res.render("index", {error: "This account does not exist !", title: "Social Application"})
}
})
})
module.exports = router;
/* addNewPost.js */
var express = require('express');
const Post = require('../models/post');
var router = express.Router();
/* GET all posts page. */
router.get('/', function(req, res, next) {
res.render("addNewPost", {title: "Social Application"})
});
router.post('/', function(req, res, next) {
var emailId = req.session.email
var postTitle = req.body.postTitle
var postMessage = req.body.postMessage
var postData = {
emailId: emailId,
postTitle: postTitle,
postMessage: postMessage
}
var aPost = new Post(postData)
aPost.save(function(err) {
if (err) {
console.log(err)
} else {
console.log("Post saved successfully !")
}
})
res.redirect("allPosts")
})
module.exports = router;

相关内容

  • 没有找到相关文章

最新更新