0

I have a two files called sample.jade and another file called insertToEnrolledSubjects.js file,(and I'm using nodejs with express framework and for db i'm using mongo db)

sample.jade file displays a table with course details. By clicking Enroll, edit ,delete button in each row of the table students can enroll for courses,edit course or delete it. So there when i click Enroll button it shows a pop up message asking about confirmation when user clicks "ok" button i need to call "insertToEnrolledSubjects" function in insertToEnrolledSubjects.js file how can i do that? Please can anyone give me a better solution i tried a lot but i didn't get any result.

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="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?");
                    if(val == true){
                        val2 = confirm("came");
                        insertToEnrolledSubjects(d,1);
                        val3 = confirm("came3");
                        }};




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


        }});
6
  • Please provide an example using jsfiddle or plnkr. Commented May 30, 2017 at 18:22
  • 1
    what you tried? Commented May 30, 2017 at 18:22
  • @Dinesh stackoverflow.com/questions/26702006/… i tried this but didn't work, sometimes i might used it wrong.I'm a beginner to nodejs. So please give me a clear solution. Commented May 30, 2017 at 18:27
  • 1
    not possible. what you are trying to do. you are calling db operations from client js file. you may create routes in your server and call from that function from server file Commented May 31, 2017 at 3:08
  • 1
    When your app is runing jade simple.jade will be render to normal html file - it is client side, but insertToEnrolledSubjects is server side. In your case, you can make a http request to do call insertToEnrolledSubjects with 2 prams courseid and enrolled. Commented May 31, 2017 at 3:12

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.