gpt4 book ai didi

javascript - ReferenceError: 'pictures' 未定义。使用 Node.js

转载 作者:太空宇宙 更新时间:2023-11-04 02:17:57 25 4
gpt4 key购买 nike

找遍了所有地方,但无法运行。我是新来的,所以对我来说搜索或修复正确的东西也有点困难。尝试从数据库中获取数据,但图片变量未定义,它说,我之前运行过它,也许只是类似的类型错误,但我已经尝试了很多东西,我的学校项目已经没有时间了。这是我的代码:

索引.ejs:

<main>
<div class="jumbotron">
<h1>Formula 1, see it, feel it!</h1></br>
<p>Welkom! Op deze site vind u de meest mooie foto's van de Formule 1.</p>
</div>
</main>

<div class="jumbotron">

<% if(pictures.length){ %>
<table>
<tr>
<th>Titel</th>
<th>Locatie</th>
<th>Datum</th>
<th>Naam</th>
<th>Waardering</th>

</tr>
<% } %>
<% for (var i = 0; i < pictures.length; i++) { %>
<tr>

<td><%= pictures[i].titel %></td>
<td><%= pictures[i].locatie %></td>
<td><%= pictures[i].datum %></td>
<td><%= pictures[i].naam %></td>
<td><%= pictures[i].waardering %></td>
<% } %>
</tr>
</table>

</div>

<footer>
<% include ../partials/footer %>
</footer>

index.js

    var express = require('express');
var router = express.Router();

router.get('/', function(req, res) {
req.getConnection(function(err, connection) {
connection.query('SELECT * FROM pictures', function(err, results) {
res.locals.pictures = results;
console.log(results);
res.render('../views/pages/index');
});
});
});

module.exports = router;

app.js:

var express = require('express'),
path = require('path'),
favicon = require('serve-favicon'),
logger = require('morgan'),
cookieParser = require('cookie-parser'),
flash = require('connect-flash'),
expressValidator = require('express-validator'),
session = require('express-session'),
passport = require('passport'),
LocalStrategy = require('passport-local').Strategy,
bodyParser = require('body-parser'),
multer = require('multer'),
mongo = require('mongodb'),
mongoose = require('mongoose'),
mysql = require('mysql'),
myConnection = require('express-myconnection');

var db = mongoose.connection;

var dbOptions = {
host: 'localhost',
user: 'fcdeen_user',
password: 'wEvGT9ctm8KUxdTD',
database: 'fcdeen'
};

var indexRoutes = require('./routes/index'),
loginRoutes = require('./routes/login'),
uploadRoutes = require('./routes/upload');

var app = express();

//Use session
app.use(session({
secret: "Its_A_veRy_DarK_SecreT",
resave: false,
saveUninitialized: true
}));

// Voeg de bodyparser middleware aan de app toe (POST requests)
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));

// set the view engine to ejs
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

// Voeg connectie middleware toe aan app
app.use(myConnection(mysql, dbOptions, 'single'));

// index page
app.get('/home', function(req, res) {
res.render('pages/index');
});

// index page
app.get('/index', function(req, res) {
res.render('pages/index');
});

// upload page
app.get('/upload', function(req, res) {
res.render('pages/upload');
});

// login page
app.get('/login', function(req, res) {
res.render('pages/login');
});

app.listen(8080);
console.log('Launch at port 8080');

这是错误:

ReferenceError: C:\School\SSS 2015\testen\node-ejs-master\views\pages\index.ejs:21
19| <div class="jumbotron">
20|
>> 21| <% if(pictures.length){ %>
22| <table>
23| <tr>
24| <th>Titel</th>

pictures is not defined
at eval (eval at <anonymous> (C:\School\SSS 2015\testen\node-ejs-master\node_modules\ejs\lib\ejs.js:242:14), <anonymous>:33:292)
at eval (eval at <anonymous> (C:\School\SSS 2015\testen\node-ejs-master\node_modules\ejs\lib\ejs.js:242:14), <anonymous>:35:68)
at C:\School\SSS 2015\testen\node-ejs-master\node_modules\ejs\lib\ejs.js:255:15
at Object.exports.render (C:\School\SSS 2015\testen\node-ejs-master\node_modules\ejs\lib\ejs.js:293:13)
at View.exports.renderFile [as engine] (C:\School\SSS 2015\testen\node-ejs-master\node_modules\ejs\lib\ejs.js:323:20)
at View.render (C:\School\SSS 2015\testen\node-ejs-master\node_modules\express\lib\view.js:126:8)
at tryRender (C:\School\SSS 2015\testen\node-ejs-master\node_modules\express\lib\application.js:639:10)
at EventEmitter.render (C:\School\SSS 2015\testen\node-ejs-master\node_modules\express\lib\application.js:591:3)
at ServerResponse.render (C:\School\SSS 2015\testen\node-ejs-master\node_modules\express\lib\response.js:961:7)
at C:\School\SSS 2015\testen\node-ejs-master\app.js:61:6
at Layer.handle [as handle_request] (C:\School\SSS 2015\testen\node-ejs-master\node_modules\express\lib\router\layer.js:95:5)
at next (C:\School\SSS 2015\testen\node-ejs-master\node_modules\express\lib\router\route.js:131:13)
at Route.dispatch (C:\School\SSS 2015\testen\node-ejs-master\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\School\SSS 2015\testen\node-ejs-master\node_modules\express\lib\router\layer.js:95:5)
at C:\School\SSS 2015\testen\node-ejs-master\node_modules\express\lib\router\index.js:277:22
at Function.process_params (C:\School\SSS 2015\testen\node-ejs-master\node_modules\express\lib\router\index.js:330:12)
at next (C:\School\SSS 2015\testen\node-ejs-master\node_modules\express\lib\router\index.js:271:10)
at C:\School\SSS 2015\testen\node-ejs-master\node_modules\express-myconnection\lib\express-myconnection.js:123:9
at Layer.handle [as handle_request] (C:\School\SSS 2015\testen\node-ejs-master\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (C:\School\SSS 2015\testen\node-ejs-master\node_modules\express\lib\router\index.js:312:13)
at C:\School\SSS 2015\testen\node-ejs-master\node_modules\express\lib\router\index.js:280:7
at Function.process_params (C:\School\SSS 2015\testen\node-ejs-master\node_modules\express\lib\router\index.js:330:12)

感谢您的帮助!

最佳答案

我认为这就是您问题的解决方案

// Don't separate the routes. If you are setting your routes in this file, try keep every route at the same file.

app.get('/', function(req, res) {
req.getConnection(function(err, connection) {
connection.query('SELECT * FROM pictures', function(err, results) {
res.locals.pictures = results;
console.log(results);
res.render('pages/index', {pictures : results});
});
});
});

// index page
app.get('/home', function(req, res) {
res.render('pages/index');
});

// index page
app.get('/index', function(req, res) {
res.render('pages/index');
});

当您输入 /home/index 时,您必须看到错误,但输入 / 应该不会有问题。

如果您尝试这三个路由执行相同的操作,您可以尝试以下操作:

app.get('/', renderPageIndex)

// index page
app.get('/home', renderPageIndex)

// index page
app.get('/index', renderPageIndex )

function renderPageIndexfunction(req, res) {
req.getConnection(function(err, connection) {
connection.query('SELECT * FROM pictures', function(err, results) {
res.locals.pictures = results;
console.log(results);
res.render('pages/index', {pictures : results});
});
});
});

关于javascript - ReferenceError: 'pictures' 未定义。使用 Node.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34873781/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com