Naresh Naresh - 2 years ago 125
SQL Question

Database update issue using node-orm2 for mysql backend

I am working on an Android app which interacts with nodejs REST server using node orm for mysql backend. On my server, I have a functionality of authenticating users based on email verification. Once verification is successful, node orm fetches the user object, changes the verified column value and saves it back.
But, the change is not reflecting in the db after execution. Only if we run the same code another time, it is reflecting in the database


exports.activateEmail = function(email, callback) {'In verifyEmailDao.js, activateEmail module');
var db = connectionObj.getConnection();
var Candidate = db.models.jobseeker_id;

Candidate.find({email : email}, function(err,candidate){
if(err){'cannot find account with email to activate', email);
callback(false, null);
else {
candidate[0].verified = true;'candidate email now activated.! status is', candidate[0].verified);
candidate[0].save(function(error){'Email verified any errors?', error);
callback(true, candidate[0].id);

Edit 1:

jobseeker_id.js (node-orm model)

var orm = require('orm');
module.exports = function(db){
console.log('coming inside candidateId.js');
var JobSeekerId = db.define('jobseeker_id', {
id : {type:'serial' , key:true},
first_name : String,
last_name : String,
email : String,
password : String,
verified : Boolean
validations : {
email : orm.enforce.unique("Already registered")

Server log:

{"name":"test-app" "msg":"In verifyEmailDao.js, activateEmail module"}
{"name":"test-app","msg":"candidate email now activated.! status is true"}
{"name":"test-app","msg":"Email verified any errors? null"}
{"name":"test-app","msg":"Email sucessfully activated. Now deleting the entry from verify email link table for candidate id 30}
{"name":"test-app","msg":"In verifyEmailDao.js, deleteRandomLink module"}
{"name":"test-app","msg":"error is---> null"}
{"name":"test-app","msg":"Entry deleted from verify email table as email is activated"}

There will no be no changes in the log when I execute the code for second time, but the change in the db will be reflected!

Answer Source

After 2 days of hell I finally fixed the issue by adding a statement db.settings.set('instance.cache', false) to the db config file. Though I did'nt clearly understand how db update issue was resolved by setting the cache to false, this did the trick!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download