我在此模型上使用 nedb 保存协议记录的应用程序存在一些问题:协议(标题、日期、患者姓名、注释)。有时近 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
});
```
这是我们的 i 执行保存到数据库中:
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();
});
}
```
欢迎任何想法,谢谢!