Answer the question
In order to leave comments, you need to log in
What could be the problem when using koa-mysql?
Good day.
I am writing (or rather rewriting with Express) an application in Koa. I use the Mysql database and the koa-mysql module ( https://github.com/eladnava/koa-mysql). And there was a problem with the following design:
exports.get=function* (next){
const ctx=this;
let sql = "SELECT * FROM clients_forums_knowledge_base ORDER BY orders ASC";
let forumsKnowledgeBase = yield ctx.locals.db.query(sql);
forumsKnowledgeBase.forEach((elem,idx) => {
let sql='SELECT clients_knowledge_base.name, clients_knowledge_base.id,clients_knowledge_base.file FROM clients_knowledge_base,clients_forums_knowledge_base WHERE clients_forums_knowledge_base.id = clients_knowledge_base.forum AND clients_knowledge_base.forum = ? ORDER BY name ASC';
let rows = yield ctx.locals.db.query(sql,elem.id);
elem.items=rows ;
})
};
app-0 /var/www/app/routes/clients/knowledgebase.js:10
app-0 let rows = yield ctx.locals.db.query(sql,elem.id);
app-0 ^^^
app-0 SyntaxError: Unexpected identifier
app-0 at exports.runInThisContext (vm.js:53:16)
app-0 at Module._compile (module.js:511:25)
app-0 at Object.Module._extensions..js (module.js:550:10)
app-0 at Module.load (module.js:456:32)
app-0 at tryModuleLoad (module.js:415:12)
app-0 at Function.Module._load (module.js:407:3)
app-0 at Function._load (/usr/lib/node_modules/pm2/node_modules/pmx/lib/transaction.js:62:21)
app-0 at Module.require (module.js:466:17)
app-0 at require (internal/module.js:20:19)
app-0 at Object.<anonymous> (/var/www/app/routes/index.js:6:43)
const koa = require('koa');
const http = require('http');
const path = require('path');
const config = require('./config');
const favicon = require('koa-favicon');
const logger = require('koa-logger');
const locals = require('koa-locals');
const serve = require('koa-static');
const body = require('koa-better-body');
const session = require('koa-generic-session');
const mysql = require('koa-mysql');
const sessionStore=("./sessionStore");
const Pug = require('koa-pug');
const locale = require('koa-locale');
const i18n = require('koa-i18n');
const router = require('./routes');
const app = koa();
locals(app, {
db: mysql.createPool(config.get('db'))
});
locale(app)
app.keys = ['KillerIsJim']
app
.use(favicon(__dirname + '/public/favicon.ico'))
.use(logger())
.use(body())
.use(session({store: sessionStore}))
.use(router.routes())
.use(router.allowedMethods())
.use(serve(__dirname + '/public'))
router.use(i18n(app, {directory: './config/locales',locales: ['en','ru-RU'],modes: ['header']}))
const pug = new Pug({viewPath: __dirname + '/views',noCache:true,app: app})
const io=require('./lib/socket');
io.attach(app)
app.listen(config.get('port'))
const router = require('koa-router')();
const koaBody = require('koa-body')()
router.redirect('/','/knowledgebase');
router.get('/knowledgebase',require('./clients/knowledgebase').get);
module.exports = router;
Answer the question
In order to leave comments, you need to log in
yield can only be used in a generator, in your nested function
it is easiest to replace forEach with a regular loop:
exports.get=function* (next){
const ctx=this;
let sql = "SELECT * FROM clients_forums_knowledge_base ORDER BY orders ASC";
let forumsKnowledgeBase = yield ctx.locals.db.query(sql);
for(let idx = 0, n = forumsKnowledgeBase.length; idx < n; idx++) {
let elem = forumsKnowledgeBase[idx];
let sql='SELECT clients_knowledge_base.name, clients_knowledge_base.id,clients_knowledge_base.file FROM clients_knowledge_base,clients_forums_knowledge_base WHERE clients_forums_knowledge_base.id = clients_knowledge_base.forum AND clients_knowledge_base.forum = ? ORDER BY name ASC';
let rows = yield ctx.locals.db.query(sql,elem.id);
elem.items=rows ;
}
};
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question