I need to call a sever side function in my jade file. I tried so many methods but didn't work. Please anyone can help me? The method that i tried lastly was using export in server side function and calling it in jade file. Sample code is shown below.
insertToEnrolledSubjects.js
var MongoClient = require('mongodb').MongoClient
, format = require('util').format;
MongoClient.connect("mongodb://localhost:27017/test", function (err, db) {
if (err)
throw err;
else {
console.log("successfully connected to the database!!!");
//calling insert function
insertToEnrolledSubjects(cid,e);
//implementation of insert function
exports.insertToEnrolledSubjects= function(courseid,enrolled) {
db.collection('EnrolledSubjects').insert({ courseId:courseid ,NoOfEnrolledStudents:enrolled});
console.log("Inserted!");
//Show total no of reults in db
db.collection('EnrolledSubjects').count(function (err, count) {
if (err) throw err;
console.log("Total Rows: " + count);
});
}
}});
Sample.jade
html
head
title="Enroll For Subjects"
link(rel='stylesheet', href='/bootstrap/css/bootstrap.min.css')
link(rel='stylesheet', href='/bootstrap/css/bootstrap-responsive.min.css')
script(src='https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js')
script(src='bootstrap/js/bootstrap.min.js')
script(src='C:/Users/Siyumi Amarasinghe/Desktop/mtit/routes/insertToEnrolledSubjects.js')
body
div.container
div.content
h2.display-4.m-b-2 Course criteria
table.table.table(border='2')
thead
tr
th Course ID
th Subject name
th Year
th Credits
th Action
tbody
- for (var i = 1; i <= 5; ++i){
tr
td=i
td="IPE"
td=i
td=5
td
button.btn.btn-danger(type='submit',onclick='Enroll("#{i}")') Enroll
td
button.btn.btn-danger(type='submit',onclick='Edit("#{i}")') Edit
td
button.btn.btn-danger(type='submit',onclick='delete1("#{i}")') Delete
- }
script.
function delete1(d){
var val = confirm("Do you want to delete this record?");
if(val == true){
window.location.href= '/delete1/'+d;
return true;
}
else{
return false;
}
};
script.
function Enroll(d){
var val = confirm("Do you really want to enroll for this course?"+d);
if(val == true){
var dt = require('C:\Users\Siyumi Amarasinghe\Desktop\mtit\routes\insertToEnrolledSubjects.js');
dt.insertToEnrolledSubjects(d,1);
}};
But i'm getting this error.
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:357:11)
at ServerResponse.header (C:\Users\Siyumi Amarasinghe\Desktop\mtit\node_modules\express\lib\response.js:730:10)
at ServerResponse.send (C:\Users\Siyumi Amarasinghe\Desktop\mtit\node_modules\express\lib\response.js:170:12)
at done (C:\Users\Siyumi Amarasinghe\Desktop\mtit\node_modules\express\lib\response.js:967:10)
at Object.exports.renderFile (C:\Users\Siyumi Amarasinghe\Desktop\mtit\node_modules\jade\lib\index.js:374:12)
at View.exports.__express [as engine] (C:\Users\Siyumi Amarasinghe\Desktop\mtit\node_modules\jade\lib\index.js:417:11)
at View.render (C:\Users\Siyumi Amarasinghe\Desktop\mtit\node_modules\express\lib\view.js:128:8)
at tryRender (C:\Users\Siyumi Amarasinghe\Desktop\mtit\node_modules\express\lib\application.js:640:10)
at EventEmitter.render (C:\Users\Siyumi Amarasinghe\Desktop\mtit\node_modules\express\lib\application.js:592:3)
at ServerResponse.render (C:\Users\Siyumi Amarasinghe\Desktop\mtit\node_modules\express\lib\response.js:971:7)
Please someone can explain me how to achieve this?.I need to call insertToEnrolledSubjects() function in "insertToEnrolledSubjects.js" file inside Enroll(d) function in sample.jade file. How can i achieve this?
Thanks in advance!!
via mad_ss
No comments:
Post a Comment