Reputation: 21
I have a table - available_trucks
where I want to update the column - available_date
.
This available date I am planing to be update every day at midnight but now for test purposes I try to update every one minute with this library in nodejs
https://www.npmjs.com/package/node-schedule
I am using also knex js as sql builder.
My code
SERVER JS( MAIN FILE )
const truckJob = require('./jobs/trucks-job');
schedule.scheduleJob('*/1 * * * *', (fireDate) => {
console.log('This job was supposed to run at ' + fireDate + ', but actually ran at ' + new Date());
truckJob.updateAvailableTrucks();
})
TRUCKS-JOB
const db = require('../db/knex');
function updateAvailableTrucks() {
db("available_trucks").update('available_date', '2020-12-13T04:47:56.126').where('truck_number',
'228');
}
So here for test purposes I am putting hardcoded date value, where truck_number
is 228. I want to mention that I checked and the data in my available_trucks
- table exist so here I am try to update existing column in my table. But after one minute when the script is again executed, the column in the table is not updated. Also when I try to get the data for example from some table then everything is working well.
For example
function getAll() {
return db.select('*').from('available_trucks');
}
My knex configuration KNEX JS
const config = require('../knexfile.js');
const pg = require('pg');
const PG_DECIMAL_OID = 1700;
// workaround that ensures numeric types are read as numbers, not strings
pg.types.setTypeParser(PG_DECIMAL_OID, parseFloat);
module.exports = require('knex')(config);
KNEX FILE JS
const config = {
client: 'pg',
connection: {
host: process.env.POSTGRES_HOST || 'localhost',
database: process.env.POSTGRES_DB || 'some_db_name',
user: process.env.POSTGRES_USER || 'postgres',
password: process.env.POSTGRES_PASSWORD || 'xx
},
pool: { min: 0, max: 10 },
migrations: { directory: './db/migrations' }
};
module.exports = config;
Upvotes: 0
Views: 2061
Reputation: 1506
It looks like your updateAvailableTrucks() function is missing an await statement when calling the database update. Since knex returns a promise, you need to await it or use .then() or .catch() or .finally() to handle the result. These methods or using the await is what triggers the Promise (and Promises in general) to be executed.
Upvotes: 0
Reputation: 21
From my short experience, for some weird reason update using knex does only works if followedby a .catch()
General example:
Does not work
knex('table')
.update({column: value})
.where({column: value});
Works
knex('table')
.update({column: value})
.where({column: value})
.catch((err) => console.log(err));
In your case try:
db("available_trucks")
.update('available_date', '2020-12-13T04:47:56.126')
.where('truck_number',
'228')
.catch((err) => console.log(err));
Upvotes: 2