Answer the question
In order to leave comments, you need to log in
Why is the data not updated when withdrawing via knex?
When outputting to /api, when I add an entry to the database and refresh the page, new data is not displayed. Those. they are output but only on server restart, in php it works without server restart so i'm a bit confused.
// connection DB
const options = {
client: 'pg',
connection: {
host: 'localhost',
user: 'postgres',
password: '1',
database: 'chat'
}
}
const webpack = require('webpack');
// const webpackDevMiddleware = require('webpack-dev-middleware');
const webpackConfig = require('./webpack.config.js');
const
knex = require('knex')(options),
Koa = require('koa'),
router = require('koa-router')(),
http = require('http'),
app = new Koa(),
serve = require('koa-static'),
server = http.createServer(app.callback()),
io = require('socket.io')(server);
knex.schema.hasTable('messages').then(exists => {
if (!exists) {
return knex.schema.createTable('messages', t => {
t.increments('id').primary();
t.string('message', 100);
t.string('idroom', 100);
});
}
});
io.on('connection', client => {
client.join('messages');
client.on('message', req => {
insertInto(req);
io.to('messages').emit('message', req.message);
});
});
let messages;
knex.select().from('messages').timeout(1000).then(res => {
messages = JSON.stringify(res);
});
router.get('/api', async (ctx, next) => {
ctx.body = messages;
});
// add value to database
function insertInto(req){
req.idroom = 'messages';
knex.insert([req], ['id']).into('messages')
.then(() => {
console.log('result:', { success: true, message: req })
});
}
app.use(serve(__dirname + '/public'))
.use(router.routes())
.use(require('webpack-hot-middleware')(webpack(webpackConfig)))
server.listen(process.env.PORT | 3000);
console.log ('Listening at port ' + 3000 + ' ...');
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question