简体   繁体   中英

Database update issue using node-orm2 for mysql backend

Problem: 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

Code

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

  Candidate.find({email : email}, function(err,candidate){
    if(err){
      log.info('cannot find account with email to activate', email);
      callback(false, null);
    }
    else {
      candidate[0].verified = true;
        log.info('candidate email now activated.! status is', candidate[0].verified);
      candidate[0].save(function(error){
        log.info('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!

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM