electron-demo icon indicating copy to clipboard operation
electron-demo copied to clipboard

Bug in database addPerson method

Open davbro63 opened this issue 7 years ago • 1 comments

The addPerson saves a new entry with properties firstName / lastName and the pre-defined database has firstname/lastname. The capitalization difference is only evident under Linux. Windows is fine.

davbro63 avatar Dec 15 '17 14:12 davbro63

I have some issue with my application made on this model to save protocol record with nedb : Protocol (title, date, patient_name, notes). Sometimes nearly 40% of times, it does not saves my record (insert or update) into the db... no error...

This is my save function :

 document.getElementById('save').addEventListener('click', () => {
        
        title = document.getElementById('title').value.trim();
        date = document.getElementById('date_picker').value.trim();
        date = (date == null) ? "" : date;
        
        duration = document.getElementById('duration').value.trim();
        cost = document.getElementById('cost').value.trim();

        name = document.getElementById('name').value.trim();
        name = (name == null) ? "" : name.trim();

        nHistory = document.getElementById('nHistory').value.trim();
        nHistory = (nHistory == null) ? "" : nHistory;

        therapy = document.getElementById('therapy').value.trim();
        therapy = (therapy == null) ? "" : therapy;

        sound = document.getElementById('sound').value.trim();
        sound = (sound == null) ? "" : sound;

        if (title == "" || date == "" || title == "" || name == "" || nHistory == "" || therapy == "" || sound == "" ){           
            dialog.showMessageBox({
                title :appName,
                type: 'warning',
                message : 'Please enter a value in all fields'
            });
            return;    

        } else {
            selectedId = document.getElementById('selectedId').value;            
            selectedId = (selectedId == null) ? "" : selectedId.trim();           
           
             // create a protocol object
            var protocol = {
                "title": title,
                "date": date,
                "duration": duration,
                "cost": cost,
                "patient_name": name,
                "nHistory": nHistory,
                "therapy": therapy,
                "sound": sound
            }

            if(selectedId == ""){ // insert query                
                database.addProtocol(protocol)  
            } else {  // update query
                database.updateProtocol({_id:selectedId}, protocol)    
            } // end if
                             
            // we redirect to the protocol list
            var url = require('url');
            var path = require('path');
            var remote = require('electron').remote;
            remote.getCurrentWindow().loadURL(url.format({
                pathname: path.join(__dirname, '../pages/protocolList.html'),
                protocol: 'file:',
                slashes: true
            }));

             // We reset the selected protocol id
            document.forms['form-edit'].selectedId = null;
            sharedObject.selectedProtocolId = "";
               
        } // end if
    });

And this is our i perform saves into the db :

var DataStore = require('nedb');

db = {};
db.users = new DataStore({filename: 'rsc/db/users.db', autoload: true });
db.protocols = new DataStore({filename: 'rsc/db/protocols.db'});


// Get a single protocol
exports.getProtocol = function(query, fnc){
    db.protocols.loadDatabase()
    db.protocols.findOne(query, function (err, doc) {  
        if(err){
            console.log("An error occured with the query : ", err); return;
        } else {
            // Execute the parameter function
            fnc(doc); 
        }                 
    });  
}

// Returns the query protocols
exports.findProtocols = function(queryParams, fnc){    
    // Get the query protocols
    db.protocols.loadDatabase();
    db.protocols.find(queryParams, function (err, docs) {
        if(err){
            console.log("An error occured with the query : ", err); return;
        } else {
            //sort protocols by date
            comparator = function(protocol1, protocol2) {
                return new Date(protocol2.date).getTime() - new Date(protocol1.date).getTime();
            }
            docs = docs.sort(comparator);
            // Execute the parameter function            
            fnc(docs);
        }
    });     
};

// Adds a protocol
exports.addProtocol = function(protocol) {    
    // save the new protocol in the database
    db.protocols.loadDatabase();
    db.protocols.insert(protocol, function(err, newProtocol){
        if(err) {           
            console.log("An error occured with the insert query", err); return;
        } else {
            console.log("protocol added...");
        }
    });
};

// Updates a protocol
exports.updateProtocol = function(where, protocol) {   
    // update the new protocol in the database
    db.protocols.loadDatabase();
    db.protocols.update(where, {$set : protocol}, {}, function(err){
        if(err) {           
            console.log("An error occured with the update query", err); return;
        } else {
            console.log("protocol updated...");
        }
    });
};


//Deletes a protocol
exports.deleteProtocol = function(queryParam, fnc){
    db.protocols.loadDatabase();
    db.protocols.remove(queryParam, {}, function(err, numRemoved){
        if(err) {           
            console.log("An error occured with the delete query", err); return;
        } else {
            console.log("protocol deleted...");
        }
        fnc();
    });
}

Any idea will be welcomed, thanks !

Nathan4KImpact avatar Jan 08 '18 16:01 Nathan4KImpact