简体   繁体   English

从服务器端向客户端节点发送数据

[英]Send data from server side to client side Node

Server side: App.js: 服务器端: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 服务器端: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 客户端: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(...). 它在Firebase.js和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. 解决方案:强制转换两次或更多次,因为我没有一个数组可以折叠Firebase中的值。

you cannot append the data on the server side, that means you cannot do this on the server $('#valuesFromDatabase').append(content); 您不能在服务器端附加数据,这意味着您不能在服务器上执行此操作$('#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' 
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM