使用nedb插入和更新有时会在我的Node JS + Electron应用程序中失败



我在此模型上制作的应用程序使用 nedb 保存协议记录时遇到了一些问题:协议(标题、日期、patient_name、注释(。有时将近 40% 的时间,它不会将我的记录(插入或更新(保存到数据库中......没有错误...

这是我的保存函数:

 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
    });
```

这是我们执行的保存到数据库中:

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();
    });
}
'

''

欢迎任何想法,谢谢!

在我实现的时间和我收到错误的时间(年初(之间,nedb 库中有一个升级,

经过调查,我只是将所有数据存储实例设置为自动加载,如下所示:

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

问题解决了。

这就是我最初希望实现它的方式,但当时它并没有像例外的那样起作用。因此,它显示为同时在库中解决的错误。

最新更新