【发布时间】:2021-10-16 06:50:04
【问题描述】:
我目前正在尝试使用可调用的 web firebase 函数从 mongodb 数据库查询中返回一些数据。函数被调用。但是,fullName 函数在我的浏览器控制台中返回 {data: null}。
我也尝试过return "test",它也返回了与{data: null}相同的值
我完全不确定这是为什么。当我尝试 console.log
const data = {
totalInfections: totalInfections,
totalUsers: totalUsers,
totalCommands: totalCommands,
totalLiveInfections: totalLiveInfections,
totalLiveInfectedChannels: totalLiveInfectedChannels
}
console.log(data)
return data
我的函数日志(在我的本地主机上)或我的终端中没有任何内容。
但是,如果我转到 http://localhost:5001/pandemic-8955f/us-central1/fullName,我的函数日志中会出现错误
function[us-central1-fullName]
{
"severity": "WARNING",
"message": "Request has invalid method. GET"
}
function[us-central1-fullName]
{
"severity": "ERROR",
"message": "Invalid request, unable to process."
}
function[us-central1-fullName]
Finished "us-central1-fullName" in ~1s
我不确定为什么会发生这种情况,任何指示或建议都会非常有帮助。
exports.fullName = functions.https.onCall((lol, lol1) => {
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/?readPreference=primary&appname=MongoDB%20Compass&ssl=false";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db("discord_pandemic");
dbo.collection("ArrayStats").find().toArray(function(err, result) {
if (err) throw err;
const totalInfections = result[0].total_infections
const totalUsers = result[0].total_users
const totalCommands = result[0].total_commands
const totalLiveInfections = result[0].total_live_infections
const totalLiveInfectedChannels = result[0].total_live_infected_channels
const data = {
totalInfections: totalInfections,
totalUsers: totalUsers,
totalCommands: totalCommands,
totalLiveInfections: totalLiveInfections,
totalLiveInfectedChannels: totalLiveInfectedChannels
}
console.log(data)
return data
});
});
});
<body>
<input type="text" id="fName" placeholder="First Name">
<input type="text" id="lName" placeholder="Last Name">
<button onClick="addName()"> Add Name </button>
<script>
function addName() {
var fullName = firebase.functions().httpsCallable('fullName');
//For the fullName we have defined that fullName takes some data as a parameter
fullName({}).then((result) => {
console.log(result);
}).catch((error) => {
console.log(error);
})
}
</script>
</body>
【问题讨论】:
标签: javascript html firebase google-cloud-firestore google-cloud-functions