// Connect to DB
const db = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '***',
database : 'db'
});
// Connect
db.connect((err) => {
if (err) {
throw err;
}
console.log('MySQL CONNECT...');
});
// Chat render
app.get('/chat', function (req, res) {
var reqDB = 'SELECT * FROM messages';
db.query(reqDB, (err, result) => {
if (err) throw err;
for(let i=0; i < result.length; i++){
result[i].text;
}
});
res.render('chat', {
smsText: result
});
});
<code lang="bash">
ReferenceError: result is not defined
at /home/bred/codes/ToDo149/app.js:66:14
at Layer.handle [as handle_request] (/home/bred/codes/ToDo149/node_modules/express/lib/router/layer.js:95:5)
at next (/home/bred/codes/ToDo149/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/home/bred/codes/ToDo149/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/bred/codes/ToDo149/node_modules/express/lib/router/layer.js:95:5)
at /home/bred/codes/ToDo149/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/home/bred/codes/ToDo149/node_modules/express/lib/router/index.js:335:12)
at next (/home/bred/codes/ToDo149/node_modules/express/lib/router/index.js:275:10)
at SendStream.error (/home/bred/codes/ToDo149/node_modules/serve-static/index.js:121:7)
at SendStream.emit (events.js:210:5)
</code>
var smsText = db.query(reqDB, (err, result) => {
if (err) throw err;
for(let i=0; i < result.length; i++){
result[i].text;
}
});
// Chat render
app.get('/chat', function (req, res) {
res.render('chat', {
smsText: result
});
});
var reqDB = 'SELECT * FROM messages';
var sms = db.query(reqDB, (err, result) => {
if (err) throw err;
console.log(result);
for(let i=0; i < result.length; i++){
var smsText = result[i].text;
console.log(smsText);
}
});
MySQL CONNECT...
[
RowDataPacket { id: 1, text: 'g' },
RowDataPacket { id: 2, text: 'rt' },
RowDataPacket { id: 3, text: 'Hi' },
RowDataPacket { id: 4, text: 'Hello' },
RowDataPacket { id: 5, text: 'Привет всем!' },
RowDataPacket { id: 6, text: 'Привет!' }
]
g
rt
Hi
Hello
Привет всем!
Привет!
const parsedSMS = JSON.parse(JSON.stringify(sms));
^
TypeError: Converting circular structure to JSON
--> starting at object with constructor 'Query'
| property '_timer' -> object with constructor 'Timer'
--- property '_object' closes the circle
at JSON.stringify (<anonymous>)
at Object.<anonymous> (/home/bred/codes/ToDo149/app.js:48:35)
at Module._compile (internal/modules/cjs/loader.js:959:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
at Module.load (internal/modules/cjs/loader.js:815:32)
at Function.Module._load (internal/modules/cjs/loader.js:727:14)
at Function.Module.runMain (internal/modules/cjs/loader.js:1047:10)
at internal/main/run_main_module.js:17:11
sudo mysql --socket=/opt/lampp/var/mysql/mysql.sock
как можно сделать этот метот по умолчанию, что бы работало по этой команже:mysql -u root -p