LunaLuna
LunaLuna

Reputation: 61

Send data from server side to client side Node

Server side: App.js:

const express = require('express')
const app = express()
var fire = require('./Firebase.js')

app.get('/route', function (req, res) {
    fire.getData(req.body, res)
})

Server side: Firebase.js

var firebase = require('firebase')
firebase.initializeApp(config);
var v = firebase.database();

var users = firebase.database().ref("users");

exports.getData = function(data, res){
    users.once('value', function(snapshot) {
        snapshot.forEach(function(childSnapshot) {

        var childData = childSnapshot.val();
   //Solution: Array if more values than one.

        var content = '';
        content += '<tr>';
        content +='<td>'+ childData.Firstname + '<td>';
        content +='<td>'+ childData.Lastname + '<td>';
        content += '</tr>';
        res.send(content);
        });
    });   
}

Client side: Structure.js

$.get("/route", function (response) { 
    console.log(response)
}) 

But the problem is that it's saying I'm trying to call it twice, casting me different error messages. So how can I get the value from server side to client side? It shows the right data in Firebase.js and in App.js whild console.log(...). I have found some posts how to do this, but it doesn't really fit my problem. Solution: casting twice or more because I didn't have an array folding the values from the firebase.

Upvotes: 1

Views: 1064

Answers (1)

garvitdelhi
garvitdelhi

Reputation: 21

you cannot append the data on the server side, that means you cannot do this on the server $('#valuesFromDatabase').append(content);

Instead, you should do this on the client like this:

$.get("/route", function (response) {
    console.log(response)
    $('#valuesFromDatabase').append(response);
    //Code here trying to print out to a table with the id                           
    'valuesFromDatabase' 
})

Upvotes: 2

Related Questions