获取错误 无法读取未定义的属性"等于"



我正在开发一个CRUD应用程序,该应用程序也允许使用Cloudinary软件包上传图像。我正在使用Node.js,express和MongoDB。在实现Cloudinary上传部分后,我的应用程序突然崩溃了。我不知道发生了什么事!我恢复了更改,但问题仍然存在。我收到错误

TypeError: C:UsersSubhadeepDocumentsWeb JobsSURJOviewsindex.ejs:66
64|             <td><%= student.author.center %></td>
65|         </tr>
>> 66|         <% }
67|                             }); %>
68| 
69|         </tbody>

Cannot read property 'equals' of undefined

我的index.ejs文件粘贴在下面

<% include ./partials/header %>
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
<!-- Content Header (Page header) -->
<section class="content-header">
<div class="container-fluid">
<div class="row mb-2">
<div class="col-sm-6">
<h1>Students List</h1>
</div>
<div class="col-sm-6">
<ol class="breadcrumb float-sm-right">
<li class="breadcrumb-item"><a href="/">Home</a></li>
<li class="breadcrumb-item active">Students</li>
</ol>
</div>
</div>
</div><!-- /.container-fluid -->
</section>
<!-- Main content -->
<section class="content">
<% if (error && error.length > 0 ) { %>
<div class="alert alert-danger" role="alert">
<%= error %>
</div>
<%}%>
<% if (success && success.length > 0 ) { %>
<div class="alert alert-info" role="alert">
<%= success %>
</div>
<%}%>
<div class="row">
<div class="col-12">

<div class="card">
<!-- /.card-header -->
<div class="card-body">
<table id="form" class="table table-bordered table-striped">
<thead>
<tr>
<th>Date of Admission</th>
<th>Name</th>
<th>Date of Birth</th>
<th>Caste</th>
<th>Contact Number</th>
<th>Registered Under</th>
</tr>
</thead>
<tbody>
<!-- Logic Will Go Here -->
<% students.forEach(function(student){
if(student.author.id.equals(currentUser._id) || currentUser.isAdmin){ %>
<tr>
<td><%= student.created.toDateString() %></td>
<td><a href="/students/<%= student._id %>"><%= student.name %></a></td>
<td><%= student.dob %></td>
<td><%= student.caste %></td>
<td><%= student.phone %></td>
<td><%= student.author.center %></td>
</tr>
<% } }); %>
</tbody>
<tfoot>
<tr>
<th>Date of Admission</th>
<th>Name</th>
<th>Date of Birth</th>
<th>Caste</th>
<th>Contact Number</th>
<th>Registered Under</th>
</tr>
</tfoot>
</table>
</div>
<!-- /.card-body -->
</div>
<!-- /.card -->
</div>
<!-- /.col -->
</div>
<!-- /.row -->
</section>
<!-- /.content -->
</div>
<script>
$(document).ready(function () {
$('#form').DataTable({
"order": [
[0, "desc"]
]
});
});
</script>
<script>
document.getElementById("viewAll").className += " active";
</script>
<!-- /.content-wrapper -->
<% include ./partials/footer %>

应用程序.js在这里

// Required Routes
var express = require("express"),
bodyParser = require("body-parser"),
mongoose = require("mongoose"),
ejs = require("ejs"),
expressSanitizer = require("express-sanitizer"),
Student = require("./models/students"),
passport = require("passport"),
localStrategy = require("passport-local"),
User = require("./models/user"),
flash = require("connect-flash"),
methodOverride = require("method-override");
var app = express();

// Routers
var authRoute = require("./routes/authentication"),
indexRoute = require("./routes/indexRoute");
//Server and Mongo and Other Setups
mongoose.connect("mongodb://localhost/surjo", {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false
});
//App config
app.set("view engine", "ejs");
app.use(express.static("public"));
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(expressSanitizer());
app.use(methodOverride("_method"));
app.use(flash());
express.static("/uploads");

// PASSPORT Configuration
app.use(require("express-session")({
secret: "Once there is something that is not found",
resave: false,
saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
passport.use(new localStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
// Routers Use
// Middleware
app.use(function (req, res, next) {
res.locals.currentUser = req.user;
res.locals.error = req.flash("error");
res.locals.success = req.flash("success");
next();
});
app.use(authRoute);
app.use(indexRoute);

//=====================================================
//Server Configuration for Node.js. Do not touch here!
// ====================================================
var port = process.env.PORT || 3000;
app.listen(port, function () {
console.log("Server Has Started!");
});
app.get("/reg-new", function (req, res) {
res.render("register-new");
})

这是索引路由文件:

var express = require("express"),
router = express.Router({
mergeParams: true
});
var Student = require("../models/students");
var User = require("../models/user");
var middleware = require("../middleware");
//RESTful Routes
// Index Route
router.get("/", middleware.isLoggedIn, function (req, res) {
res.redirect("/students");
})
// View All Students
router.get("/students", middleware.isLoggedIn, function (req, res) {
Student.find({}, function (err, students) {
if (err) {
req.flash("error", "Sorry! Something went wrong!");
console.log(err);
} else {
res.render("index", {
students: students,
currentUser: req.user,
});
}
});
});
//NEW Route
router.get("/students/new", middleware.isLoggedIn, function (req, res) {
res.render("new");
})
// CREATE Route
router.post("/students", middleware.isLoggedIn, function (req, res) {
req.body.student.body = req.sanitize(req.body.student.body);
Student.create(req.body.student, function (err, newStudent) {
if (err) {
res.render("/new");
} else {
newStudent.author.id = req.user._id;
newStudent.author.username = req.user.username;
newStudent.author.center = req.user.center;
newStudent.save();
res.redirect("/students");
}
});
});

//SHOW Route
router.get("/students/:id", middleware.isLoggedIn, function (req, res) {
Student.findById(req.params.id, function (err, foundStudent) {
if (err) {
req.flash("error", "Sorry! Something Went Wrong!")
res.redirect("/students");
} else {
if (foundStudent.author.id.equals(req.user._id) || req.user.isAdmin) {
res.render("show", {
student: foundStudent
});
} else {
req.flash("error", "You are not authorized to view.");
res.redirect("/students");
}
}
});
});

//EDIT Route
router.get("/students/:id/edit", middleware.checkStudentOwnership, function (req, res) {
// is user logged in
Student.findById(req.params.id, function (err, foundStudent) {
if (foundStudent.author.id.equals(req.user._id)) {
res.render("edit", {
student: foundStudent
});
} else {
res.render("/students/:id", {
error: "You are not authorised."
});
}
});
});
// UPDATE Route
router.put("/students/:id", middleware.isLoggedIn, function (req, res) {
req.body.student.body = req.sanitize(req.body.student.body);
Student.findByIdAndUpdate(req.params.id, req.body.student, function (err, foundStudent) {
if (foundStudent.author.id.equals(req.user._id)) {
res.redirect("/students/" + req.params.id);
} else {
res.redirect("/index");
}
});
});
//DESTROY Route
router.delete("/students/:id", middleware.checkStudentOwnership, function (req, res) {
Student.findByIdAndRemove(req.params.id, function (err) {
if (err) {
res.redirect("/index");
} else {
req.flash("success", "Student Deleted.");
res.redirect("/students/");
}
})
})
router.get("/login", function (req, res) {
res.render("login");
})
// Export
module.exports = router;

我已经花了将近 24 小时,但仍然无法解决!希望在这里得到解决方案。谢谢!

下次我建议谷歌搜索javascript Cannot read property of undefined(基本上是你得到的错误,但没有变量名称(。我怀疑Stack Overflow上的许多人会认为这是一个低质量的问题。

现在,为了一个明确的答案:

Cannot read property 'equals' of undefined意味着 Javascript 解释器正在尝试访问未定义变量中的属性equals

Typerror bla ba index.ejs:66意味着从Javascript的角度来看,错误发生在第66行。在您的情况下,看起来 ejs 的东西有点弄乱了行号,但如果您进行调查,您会发现您正在第 56 行呼叫student.author.id.equals。如果对此进行调试,您会发现student.author.id可能由于各种原因之一而undefined

最新更新