[英]How to get referrer URL from remote domain when using socket.io and a static index.html
[英]How to get response from firebase function to firebase static index.html
我试图在index.html的firebase静态文件中获取我的firebase数据库响应。 在firebase index.js中,我正在获取数据库并通过getUsers方法返回所有ref,然后通过promise(then)方法从getusers获取响应,从而将我的res.send(res)发送到firebase静态文件的index.html。 但是在getajax调用中没有得到数据参数的任何响应,而是在status参数中获得了scucess状态。
firebase folder structure
functions
|_ index.js
public
|_index.html
Index.html -- firebase public index.hmlt
<script>
const _url_prod = "https://nodejs-fef5b.firebaseapp.com/get";
$('button').click(function(){
$.get(_url_prod,(data,status)=>{
console.log("data",data);
console.log("status",status)
})
</script>
index.js-- firebase function folder has index.js
var admin = require("firebase-admin");
var serviceAccount = require("./serviceAccountKey.json");
const firebaseApp = admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://nodejs-crud-12758.firebaseio.com"
});
const functions = require('firebase-functions');
const express = require('express');
const app = express();
async function getUsers(){
const ref = await firebaseApp.database().ref('users');
return ref.once('value').then(data => {
data.val();
})
}
app.get('/get',(req,res)=>{
getUsers().then(items => {
res.send(items);
});
})
exports.app = functions.https.onRequest(app);
```
Index.html:
<script
src="https://code.jquery.com/jquery-3.4.1.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
crossorigin="anonymous"></script>
<script>
$.get('http://192.168.56.1:5555/get', (data, success) => {
console.log(data);
console.log(success);
})
</script>
你的脚本:
async function getUsers() {
return await
admin.database().ref('Users').once('value').then((snapshot) => {
return snapshot.val();
}).catch((error) => {
console.log(error);
})
}
app.get('/get', (req, res, next) => {
getUsers().then((result) => {
console.log(result);
res.send(result);
})
})
这对我有用。 希望它可以提供帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.