Я получаю следующую ошибку при попытке обновить сообщение в моем приложении node / express. Я также использую 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!");
});