Google firebase可调用web函数-返回{data: null}



我目前正在尝试使用可调用的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>

在您的代码中有异步调用,一个打开到Mongo的连接,然后另一个从Mongo加载数据。当数据加载完成,return data执行时,Function的close}早就执行了——没有人会看到这个返回值。

在大多数情况下,您可以在启动异步操作时从调用的API中获得通常的承诺。但似乎MongoClient.connect不返回承诺,所以你必须像这样自己的构造函数:

exports.fullName = functions.https.onCall((lol, lol1) => {
return new Promise((resolve, reject) => {  // 👈 return a promise, so Cloud Functions knows to wait
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)
resolve(data); // 👈 resolve the promise with the data for the user
});
});
});
});

由于您不熟悉这种异步行为,我强烈建议您检查:

  • 关于何时终止云功能的Firebase文档。

最新更新