当我尝试在我的节点/快速应用中更新帖子时出现以下错误。我也在使用mongoDB。
Cannot read property 'equals' of undefined
at eval (eval at compile
(/home/ubuntu/workspace/node_modules/ejs/lib/ejs.js:549:12),
<anonymous>:78:42)
at returnedFn
(/home/ubuntu/workspace/node_modules/ejs/lib/ejs.js:580:17)
at tryHandleCache
(/home/ubuntu/workspace/node_modules/ejs/lib/ejs.js:223:34)
at View.exports.renderFile [as engine]
(/home/ubuntu/workspace/node_modules/ejs/lib/ejs.js:437:10)
这里是弹出错误的地方
<% if(currentUser &&
bodhi.author.id.equals(currentUser._id) || currentUser &&
currentUser.isAdmin === true){ %>
这可能与我正在运行的节点/ npm版本有关吗?
我正在使用节点-v v10.9.0和npm -v 6.4.1
我在使用以下应用程序的应用程序上具有类似的功能 v10.5.0和npm -v 6.2.0
这是我的模特:
var mongoose = require("mongoose");
var bodhiSchema = new mongoose.Schema({
name: String,
donation: String,
BTCaddress: String,
image: String,
imageId: String,
description: String,
createdAt: { type: Date, default: Date.now },
author: {
id: {
type: mongoose.Schema.Types.ObjectId,
ref: "User"
},
username: String
},
comments: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "Comment"
}
]
});
module.exports = mongoose.model("Bodhi", bodhiSchema);
这是错误所指向的块:
<div class="col-md-12">
<div class="row">
<div class="thumbnail">
<img class="img-responsive" src="<%= bodhi.image %>">
<div class="caption-full">
<h4>suggested donation <%= bodhi.donation %>/BTC<h4>
<hr>
<h5><a><%=bodhi.name%></a></h5>
<p><%= bodhi.description %></p>
<p>
<em>Submitted by: <%= bodhi.author.username %>, <%=
moment(bodhi.createdAt).fromNow() %></em>
</p>
<% if(currentUser &&
bodhi.author.id.equals(currentUser._id) || currentUser
&& currentUser.isAdmin === true){ %>
<a class="btn btn-xs btn-warning"
href="/bodhis/<%=bodhi._id %>/edit">Edit</a>
<form class="delete-form"
action="/bodhis/<%=bodhi._id %>?_method=DELETE"
method="POST"
onsubmit="return confirm('Are You Sure You
Want To Delete This Bodhi?');">
<button class="btn btn-xs btn-
danger">Delete</button>
</form>
<% }%>
</div>
</div>
</div>
这是我的app.js文件:
var dotenv = require('dotenv').config(),
express = require("express"),
app = express(),
bodyParser = require("body-parser"),
mongoose = require("mongoose"),
flash = require("connect-flash"),
passport = require("passport"),
cookieParser = require("cookie-parser"),
LocalStrategy = require("passport-local"),
methodOverride = require("method-override"),
Bodhi = require("./models/bodhi"),
Comment = require("./models/comment"),
User = require("./models/user"),
session = require("express-session"),
moment = require('moment'),
seedDB = require("./seeds"),
//requiring routes
commentRoutes = require("./routes/comments"),
bodhiRoutes = require("./routes/bodhis"),
indexRoutes = require("./routes/index"),
contactRoutes = require("./routes/contact");
console.log(process.env.DATABASEURL);
var url = process.env.DATABASEURL ||
"mongodb://localhost/bodhishare";
mongoose.connect(url);
app.use(bodyParser.urlencoded({extended: true}));
app.set("view engine", "ejs");
app.use(express.static(__dirname + "/public"));
app.use(methodOverride("_method"));
app.use(cookieParser());
app.use(session({secret: process.env.secret}));
app.use(flash());
//seedDB(); //uncomment this line to seed the database
app.locals.moment = require("moment");
//passport config
app.use(require("express-session")({
secret: process.env.secret,
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());
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("/", indexRoutes);
app.use("/bodhis", bodhiRoutes);
app.use("/bodhis/:id/comments", commentRoutes);
app.use("/contact", contactRoutes);
app.listen(process.env.PORT, process.env.IP ,function(){
console.log("The bodhishare server has started!");
});