大佬教程收集整理的这篇文章主要介绍了无法将 JSON 对象传递给 EJS 文件,我一直未定义,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我目前正在尝试设置一个 Mongoose/Node 应用程序,以了解如何更好地使用数据库。具体来说,我试图将一个 JsON 对象传递给我的一个 EJs 文件,而我试图呈现的对象是未定义的。我正在使用来自 MongoDB Atlas 的示例电影数据库,并且我知道我已成功连接它,因为当我在节点中运行我的 index.Js 文件时,我设置的 .then() 将成功消息记录到控制台。这是我在 index.Js 中的设置:
const app = express();
const path = @R_874_10613@ire('path');
const mongoose = @R_874_10613@ire('mongoose');
const methodoverrIDe = @R_874_10613@ire('method-overrIDe');
const uri = 'mongodb+srv://MYUSERname:MYpassworD@cluster0.9a2me.mongodb.net/sample_mflix?retryWrites=true&w=majority';
const MovIE = @R_874_10613@ire('./Models/movIE');
app.use(expresS.Urlencoded({ extended: true }));
app.use(methodoverrIDe('_method'));
app.set('vIEws',path.join(__dirname,'vIEws'));
app.set('vIEw ENGIne','eJs');
mongoose
.connect(uri,{ useNewUrlParser: true,useUnifIEdtopology: true })
.then(() => {
console.log('Mongo Connection Open!');
})
.catch(err => {
console.log('MONGO ERROR:',err);
});
app.set('vIEws','eJs');
这是我在不同文件中制作的模型:
//MovIE model
const mongoose = @R_874_10613@ire('mongoose');
const movIEsscheR_374_11845@a = new mongoose.scheR_374_11845@a(
{
plot: {
type: String
},genres: [
{
type: String
}
],runtime: {
type: number
},cast: [
{
type: String
}
],num_mflix_comments: {
type: number
},title: {
type: String
},countrIEs: [
{
type: String
}
],released: {
type: Date
},directors: [
{
type: String
}
],rated: {
type: String
},aWARDs: {
wins: {
type: number
},nominations: {
type: number
},text: {
type: String
}
},LASTUPDATED: {
type: String
},year: {
type: number
},imdb: {
raTing: {
type: number
},Votes: {
type: number
},ID: {
type: number
}
},type: {
type: String
},tomatoes: {
vIEwer: {
raTing: {
type: number
},numRevIEws: {
type: number
},meter: {
type: number
}
},LASTUPDATED: {
type: Date
}
}
},{ collection: 'movIEs' }
);
const MovIE = mongoose.model('MovIE',movIEsscheR_374_11845@a);
module.exports = MovIE;
最后,我试图简单地渲染电影标题的 get 路径:
app.get('/',async (req,res) => {
const movIEs = await MovIE.find({ title: 'Amadeus' });
res.render('index',{ movIEs });
});
<!DOCTYPE HTML>
<HTML lang="en">
<head>
<Meta charset="UTF-8">
<Meta name="vIEwport" content="wIDth=device-wIDth,initial-scale=1.0">
<title>Index</title>
</head>
<body>
<h1>Index</h1>
<p><%=movIEs.title%></p>
</body>
</HTML>
我相信我已经正确使用了 EJs 语法。我一遍又一遍地查看了我制作的模型,并尝试在每个文件中使用大量 console.log() 进行调试。我似乎无法找到我的错误。将不胜感激!
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)
以上是大佬教程为你收集整理的无法将 JSON 对象传递给 EJS 文件,我一直未定义全部内容,希望文章能够帮你解决无法将 JSON 对象传递给 EJS 文件,我一直未定义所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。