Мне нужно получить все старые данные формы, прежде чем обновлять их до новых. Мне нужны эти данные формы для целей отчетности. В основном это будет версия данных.
Я работаю над #Nodejs #ExpressJS #EJS в качестве шаблона и # MONGODB, # mongoose.
Ниже приведены изображения кода! Я новичок в программировании, поэтому, пожалуйста, помогите мне с частью кодирования, не описывайте это, используя кодирование Jargans!
- Фрагмент схемы
var mongoose = require('mongoose');
var bcrypt = require('bcrypt-nodejs');
// define the schema for our user model
var procedureschema = mongoose.Schema({
proceduretype : String,
procedurename : String,
procedurenumber : String,
scope : String,
status:{type:String,lowercase:true,enum:["draft", "active", "pending"]},
forms : String,
relatedprocedure : String,
norms : String,
qualityrecords : String,
expdate : String,
companyname : String,
version : String,
owner: String,
approver : String,
procedurefiles : String,
flowchart : String,
proceduredescription : String,
isotags : String,
departmentname : String
});
// generating a password hash
// create the model for users and expose it to our app
module.exports = mongoose.model('Procedure', procedureschema);
Форма просмотра страницы EJS
<% include sidebar.ejs %>
<div class="dt-content">
<!-- Page Header -->
<!-- <div class="dt-page__header">
<h1 class="dt-page__title">New Procedure</h1>
</div> -->
<!-- /page header -->
<!-- Grid -->
<div class="row">
<!-- Grid Item -->
<div class="col-xl-12">
<!-- Card -->
<div class="dt-card">
<div class="dt-card__header">
<!-- Card Heading -->
<div class="dt-card__heading">
<h3 class="dt-card__title">New Procedure</h3>
</div>
<!-- /card heading -->
<a type="button" href="/admin/add/procedure/forms" role="button" class="btn btn-dark mr-2 mb-2"> Add Forms</a>
<a type="button" href="/admin/add/procedure/type" role="button" class="btn btn-dark mr-2 mb-2"> Add Procedure Type</a>
<a type="button" href="/admin/add/procedure/norms" role="button" class="btn btn-dark mr-2 mb-2"> Add Norms</a>
<a type="button" href="/admin/add/procedure/tags" role="button" class="btn btn-dark mr-2 mb-2"> Add Procedure Tags</a>
</div>
<!-- Card Header -->
<!-- /card header -->
<div class="dt-card__body">
<form action="/admin/create/procedure" method="post" enctype="multipart/form-data">
<div class="form-row">
<div class="col-sm-6 mb-3">
<label>Procedure Type</label>
<div class="input-group">
<select class="custom-select" name="proceduretype" id="inputGroupSelect04">
<option selected>Choose Procedure Type</option>
<% procedurecat.forEach(function(data){%>
<option value="<%=data.category_name %>"><%=data.category_name %></option>
<%})%>
</select>
</div>
</div>
<div class="col-sm-6 mb-3">
<label>Procedure Name</label>
<input type="text" class="form-control" id="validationDefault03" name="procedurename" placeholder="Enter Procedure Name"
required>
</div>
<div class="col-sm-6 mb-3">
<label>Procedure Number</label>
<input type="number" class="form-control" name="procedurenumber" id="validationDefault03" placeholder="Enter Procedure Number"
required>
</div>
<div class="col-sm-6 mb-3">
<label>Scope</label>
<input type="text" class="form-control" id="validationDefault03" name="scope" placeholder="Enater Procedure Scope"
required>
</div>
<div class="col-sm-6 mb-3">
<label>Status</label>
<div class="input-group">
<select class="custom-select" name="status" id="inputGroupSelect04">
<option selected>Choose Status</option>
<option value="draft">Draft</option>
<option value="pending">Pending</option>
<option value="active">Active</option>
</select>
</div>
</div>
<div class="col-sm-6 mb-3">
<label>Forms</label>
<div class="input-group">
<select class="custom-select" name="forms" id="inputGroupSelect04">
<option selected>Choose Forms</option>
<% forms.forEach(function(data){%>
<option value="<%=data.forms_name %>"><%=data.forms_name %></option>
<%})%>
</select>
</div>
</div>
<div class="col-sm-6 mb-3">
<label>Related Procedure</label>
<div class="input-group">
<select class="custom-select" name="relatedprocedure" id="inputGroupSelect04">
<option selected>Choose Related Procedures</option>
<% proceduretitle.forEach(function(data){%>
<option value="<%=data.procedurename %>"><%=data.procedurename %></option>
<%})%>
</select>
</div>
</div>
<div class="col-sm-6 mb-3">
<label>Select Norms</label>
<div class="input-group">
<select class="custom-select" name="norms" id="inputGroupSelect04">
<option selected>Choose Norms</option>
<% norms.forEach(function(data){%>
<option value="<%=data.norms_name %>"><%=data.norms_name %></option>
<%})%>
</select>
</div>
</div>
<div class="col-sm-6 mb-3">
<label>Quality Records</label>
<input type="text" name="qualityrecords" class="form-control" id="validationDefault03"
placeholder="Enter Quality Records Here" required>
</div>
<div class="col-sm-6 mb-3">
<label>Expiration Date</label>
<div class="input-group date" id="date-time-picker-1" data-target-input="nearest">
<input type="text" name = "expdate" class="form-control datetimepicker-input" data-target="#date-time-picker-1" />
<div class="input-group-append" data-target="#date-time-picker-1" data-toggle="datetimepicker">
<div class="input-group-text"><i class="icon icon-calendar"></i></div>
</div>
</div>
</div>
<div class="col-sm-6 mb-3">
<label>Company Name</label>
<input type="text" value="<%= user.companyname %>" name="companyname" class="form-control" id="validationDefault03" readonly
>
</div>
<div class="col-sm-6 mb-3">
<label>Version</label>
<input type="number" step ="0.1" name="version" class="form-control" id="validationDefault03" placeholder="Auto Increased field"
required>
</div>
<div class="col-sm-6 mb-3">
<label>Owner</label>
<div class="input-group">
<select class="custom-select" name="owner" id="inputGroupSelect04">
<option selected>Choose Owner</option>
<% users.forEach(function(data){%>
<option value="<%=data.name %>"><%=data.name %></option>
<%})%>
</select>
</div>
</div>
<div class="col-sm-6 mb-3">
<label>Approver</label>
<div class="input-group">
<select class="custom-select" name="approver" id="inputGroupSelect04">
<option selected>Choose Approver</option>
<% users.forEach(function(data){%>
<option value="<%=data.name %>"><%=data.name %></option>
<%})%>
</select>
</div>
</div>
<div class="col-sm-6 mb-3">
<label>Departmnent Name</label>
<div class="input-group">
<select class="custom-select" name="departmentname" id="inputGroupSelect04">
<option selected>Choose Department Name</option>
<% department.forEach(function(data){%>
<option value="<%=data.departmentname %>"><%=data.departmentname %></option>
<%})%>
</select>
</div>
</div>
<div class="col-sm-12 mb-3 form-row">
<label class=" col-form-label text-sm-left" for="file-field">Attached Files</label>
<div class="col-md-12 col-sm-9">
<div class="custom-file">
<input type="file" name="procedurefiles" onchange="GetFileSizeNameAndType()" id="file" class="custom-file-input" id="file-field" multiple>
<label class="custom-file-label" for="file-field">Attacthed your files here ..</label>
</div>
</div>
<div class="form-row input-group">
<div class="col-md-6 col-sm-6 form-row">
<div id="fp"></div>
</div>
</div>
</div>
<div class=" col-sm-12 mb-4 ">
<label class=" text-sm-left mb-4">Flowchart</label>
<button type="button" href="javascript:void(0)" role="button" class="btn btn-dark mr-2 mb-2">Design your flowchart</button>
</div>
<div class=" col-sm-12 mb-4 ">
<label>Description</label>
<textarea class=" col-sm-12 mb-4" name="proceduredescription" id="summernote"></textarea>
</div>
</div>
<hr class="border-dashed my-8">
<div class=" form-row mb-4 ">
<label class="col-md-2 col-sm-3 text-sm-right mb-4 mb-sm-0">Read Confirmation</label>
<div class="col-md-10 col-sm-9">
<!-- Checkbox -->
<div class="custom-control custom-checkbox custom-control-inline">
<input type="checkbox" id="customcheckboxInline1" name="customcheckboxInline1"
class="custom-control-input" checked>
<label class="custom-control-label" for="customcheckboxInline1"></label>
</div>
</div>
</div>
<hr class="border-dashed my-8">
<div class="form-group form-row mb-0 col-md-12">
<button type="submit" class="btn btn-primary text-uppercase">Submit this procedure</button>
</div>
</form>
</div>
<!-- Card Body -->
</div>
<!-- /card body -->
</div>
<!-- /card -->
</div>
<!-- /grid item -->
</div>
<!-- /grid -->
<!-- /site content -->
<% include footer.ejs %>
<script>
$(document).ready(function() {
$('#summernote').summernote();
});</script>
<script>
function GetFileSizeNameAndType()
{
var fi = document.getElementById('file'); // GET THE FILE INPUT AS VARIABLE.
var totalFileSize = 0;
// VALIDATE OR CHECK IF ANY FILE IS SELECTED.
if (fi.files.length > 0)
{
// RUN A LOOP TO CHECK EACH SELECTED FILE.
for (var i = 0; i <= fi.files.length - 1; i++)
{
//ACCESS THE SIZE PROPERTY OF THE ITEM OBJECT IN FILES COLLECTION. IN THIS WAY ALSO GET OTHER PROPERTIES LIKE FILENAME AND FILETYPE
var fsize = fi.files.item(i).size;
totalFileSize = totalFileSize + fsize;
document.getElementById('fp').innerHTML = document.getElementById('fp').innerHTML + '<br /> ' + 'File Name is <b>' + fi.files.item(i).name + "</b>.";
}
}
document.getElementById('divTotalSize').innerHTML = "Total File(s) Size is <b>" + Math.round(totalFileSize / 1024) + "</b> KB";
}
</script>
Маршруты этой формы Post и Update API
app.post('/create/procedure', function (req, res) {
let proceduresinglefiles = req.files.procedurefiles
if(Array.isArray(proceduresinglefiles)==false){
console.log(Array.isArray(proceduresinglefiles));
console.log(typeof(proceduresinglefiles));
console.log(proceduresinglefiles);
var filename = proceduresinglefiles.name;
proceduresinglefiles.mv('./public/assets/documents/procedure/' + filename, function(err) {
if (err)
console.log(err);
});
var procedure = Procedure();
procedure.proceduretype = req.body.proceduretype;
procedure.procedurename = req.body.procedurename;
procedure.procedurenumber = req.body.procedurenumber;
procedure.scope = req.body.scope;
procedure.status = req.body.status;
procedure.forms = req.body.forms;
procedure.relatedprocedure = req.body.relatedprocedure;
procedure.norms = req.body.norms;
procedure.qualityrecords = req.body.qualityrecords;
procedure.expdate = req.body.expdate;
procedure.companyname = req.body.companyname;
procedure.version = req.body.version;
procedure.owner = req.body.owner;
procedure.approver = req.body.approver;
procedure.procedurefiles = filename;
procedure.flowchart = req.body.flowchart;
procedure.proceduredescription = req.body.proceduredescription;
procedure.isotags = req.body.isotags;
procedure.departmentname = req.body.departmentname
procedure.save(function (err, getprocedure) {
if (err) res.json(err);
else {
console.log(getprocedure);
res.redirect("/admin/view/procedure");
}
})
}
else
{
var filenamea=[];
for(var i=0;i<proceduresinglefiles.length;i++){
console.log(proceduresinglefiles.length);
console.log(i);
var procedurefilename = proceduresinglefiles[i].name;
console.log(procedurefilename);
filenamea[i]=procedurefilename;
console.log(filenamea);
proceduresinglefiles[i].mv('./public/assets/documents/procedure/' + procedurefilename, function(err) {
if (err)
console.log(err);
});
}
let multipleprocedurefilename= filenamea.toString();
var procedure = Procedure();
procedure.proceduretype = req.body.proceduretype;
procedure.procedurename = req.body.procedurename;
procedure.procedurenumber = req.body.procedurenumber;
procedure.scope = req.body.scope;
procedure.status = req.body.status;
procedure.forms = req.body.forms;
procedure.relatedprocedure = req.body.relatedprocedure;
procedure.norms = req.body.norms;
procedure.qualityrecords = req.body.qualityrecords;
procedure.expdate = req.body.expdate;
procedure.companyname = req.body.companyname;
procedure.version = req.body.version;
procedure.owner = req.body.owner;
procedure.approver = req.body.approver;
procedure.procedurefiles = multipleprocedurefilename;
procedure.flowchart = req.body.flowchart;
procedure.proceduredescription = req.body.proceduredescription;
procedure.isotags = req.body.isotags;
procedure.departmentname = req.body.departmentname
procedure.save(function (err, getprocedure) {
if (err) res.json(err);
else {
console.log(getprocedure);
res.redirect("/admin/view/procedure");
}
})
}
});
app.post('/edit/procedure/:id',(req, res) => {
Procedure.findOneAndUpdate({_id:req.params.id},{
$set:{
proceduretype : req.body.proceduretype,
procedurename : req.body.procedurename,
procedurenumber : req.body.procedurenumber,
scope : req.body.scope,
status : req.body.status,
forms : req.body.forms,
relatedprocedure : req.body.relatedprocedure,
norms : req.body.norms,
qualityrecords : req.body.qualityrecords,
expdate : req.body.expdate,
companyname : req.body.companyname,
version : req.body.version,
owner : req.body.owner,
approver : req.body.approver,
procedurefiles : req.body.procedurefiles,
flowchart : req.body.flowchart,
proceduredescription : req.body.proceduredescription,
isotags : req.body.isotags,
departmentname : req.body.departmentname
}},
{new: true, upsert:true},
function(err, procedurenew){
if(err){
console.log(err);
}
else
{
console.log(procedurenew);
res.redirect('/admin/view/procedure')
}
}
)
});