elkegovaert
elkegovaert

Reputation: 1

Using Ajax with Mongodb

I've been looking for this for quite a while and I have no clue. I want to use Ajax to display information from a collection called 'reizen' in mondodb. I want to be able switch through all the elements from the collection and get all the information like the title etc. I have no idea how to get the information, I allready have a bit of code but I've been stuck for days, so I hope somebody can help me. If I only know how to get the title from each element from the collection 'reizen', I will be able to work further. (I copied some code from someone who used CD's but I want to do the same with 'reizen')

This is my code:

extends layout

block content

    #reizen.w3-content.w3-container.w3-padding-64
        h3.w3-center REIZEN

        //reizen    
        input(type='button' onclick='previous()' value='<<')
        input(type='button' onclick='next()' value='>>')
        p
            |
        #showCD Here we will see all the elements of collection 'reizen'

    block content
    script.
        var i = 0;
        var x;
        displayCD(i);
        function displayCD(i) {
            var xmlhttp = new XMLHttpRequest();
            xmlhttp.onreadystatechange = function() {
                if (this.readyState == 4 && this.status == 200) {
                    myFunction(this, i);
                }
            };
            xmlhttp.open("GET", "cd_catalog.xml", true);
            xmlhttp.send();
        }

        function myFunction(xml, i) {
            var xmlDoc = xml.responseXML;
            x = xmlDoc.getElementsById('showCD');
            document.getElementById('showCD').innerHTML =
            "Title:"  // get the title?
        }

        function next() {
            if (i < x.length-1) {
                i++;
                displayCD(i);
            }
        }
        function previous() {
            if (i > 0) {
                i--;
                displayCD(i);
            }
        }

Upvotes: 0

Views: 34

Answers (1)

zero298
zero298

Reputation: 26909

You are trying to use the data server-side. Your AJAX will work client-side. You can use Jade/Pug to create the container to hold your CDs, but you have to actually create the DOM elements client side using createElement, or React, or whatever other DOM API/wrapper you want.

If you want all of this to be done server side, you don't want to do your AJAX in a script block that gets served to the client, you want to get the CDs programmatically within server-land and then pass the CD data to the Jade/Pug renderer server-side.

You are trying to mix server-land and client-land and you need to pick one.

Upvotes: 1

Related Questions