gpt4 book ai didi

javascript - 从服务器端向客户端 Node 发送数据

转载 作者:行者123 更新时间:2023-11-30 14:47:09 25 4
gpt4 key购买 nike

服务器端: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)
})

服务器端: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);
});
});
}

客户端:Structure.js

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

但问题是它说我试图调用它两次,向我转换不同的错误消息。那么如何从服务器端获取值到客户端呢?它在 Firebase.js 和 App.js whild console.log(...) 中显示正确的数据。我找到了一些如何执行此操作的帖子,但它并不真正适合我的问题。解决方案:转换两次或更多次,因为我没有折叠来自 firebase 的值的数组。

最佳答案

你不能在服务器端附加数据,这意味着你不能在服务器上这样做$('#valuesFromDatabase').append(content);

相反,您应该像这样在客户端上执行此操作:

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

关于javascript - 从服务器端向客户端 Node 发送数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48733208/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com