作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要确保在定义 firebase 函数之前运行此代码,因为它取决于代码中设置的变量:
const hotelBedTimeouts = [];
var beds = db.ref('/beds');
// Initialise the bed timeout holder object
beds.once("value", function(snapshot){
var hotels = snapshot.val();
for (var i = 0; i < hotels.length; i++) {
// push empty list to be filled with lists holding individual bed timeouts
if(hotels[i]){
hotelBedTimeouts.push([]);
for(var j = 0; j < hotels[i].length; j++) {
// this list will hold all timeouts for this bed
hotelBedTimeouts[i].push({});
}
} else {
hotelBedTimeouts.push(undefined);
}
}
});
有人建议我将此函数放在 once()
调用之后的 .then()
语句中。所以我尝试了这个:
const hotelBedTimeouts = [];
var beds = db.ref('/beds');
// Initialise the bed timeout holder object
beds.once("value", function(snapshot){
var hotels = snapshot.val();
for (var i = 0; i < hotels.length; i++) {
// push empty list to be filled with lists holding individual bed timeouts
if(hotels[i]){
hotelBedTimeouts.push([]);
for(var j = 0; j < hotels[i].length; j++) {
// this list will hold all timeouts for this bed
hotelBedTimeouts[i].push({});
}
} else {
hotelBedTimeouts.push(undefined);
}
}
}).then( () => {
// Frees a bed after a set amount of time
exports.scheduleFreeBed = functions.database.ref('/beds/{hotelIndex}/{bedIndex}/email').onUpdate( (snapshot, context) => {
// My code
});
不幸的是,这导致我的整个 firebase 函数被删除:
$ firebase deploy --only functions
=== Deploying to 'company-23uzc'...
i functions: deleting function scheduleFreeBed...
✔ functions[scheduleFreeBed]: Successful delete operation.
是否可以通过这种方式定义 firebase 函数?
如何确保 Firebase 函数始终可以访问后端代码中定义的某些变量?
编辑:
这是我在道格·史蒂文森回答之后第一次尝试解决方案:
const hotelBedTimeouts = [];
var beds = db.ref('/beds');
const promise = beds.once("value");
// Frees a bed after a set amount of time
exports.scheduleFreeBed = functions.database.ref('/beds/{hotelIndex}/{bedIndex}/email').onUpdate( (snapshot, context) => {
promise.then( (snapshot) => {
var hotels = snapshot.val();
for (var i = 0; i < hotels.length; i++) {
// push empty list to be filled with lists holding individual bed timeouts
if(hotels[i]){
hotelBedTimeouts.push([]);
for(var j = 0; j < hotels[i].length; j++) {
// this list will hold all timeouts for this bed
hotelBedTimeouts[i].push({});
}
} else {
hotelBedTimeouts.push(undefined);
}
}
});
var originalEmail = snapshot.after.val();
var hotelIndex = context.params.hotelIndex;
var bedIndex = context.params.bedIndex;
if (originalEmail === -1) {
clearTimeout(hotelBedTimeouts[hotelIndex][bedIndex].timeoutFunc); // clear current timeoutfunc
return 0; // Do nothing
}
// replace old timeout function
hotelBedTimeouts[hotelIndex][bedIndex].timeoutFunc = setTimeout(function () { // ERROR HERE
var bedRef = admin.database().ref(`/beds/${hotelIndex}/${bedIndex}`);
bedRef.once("value", function(bedSnap){
var bed = bedSnap.val();
var booked = bed.booked;
if (!booked) {
var currentEmail = bed.email;
// Check if current bed/email is the same as originalEmail
if (currentEmail === originalEmail) {
bedSnap.child("email").ref.set(-1, function() {
console.log("Freed bed");
});
}
}
});
}, 300000); // 5 min timeout
return 0;
});
不过,函数执行时似乎 hotelBedTimeouts 尚未正确定义,请查看以下错误:
TypeError: Cannot read property '15' of undefined
我已在代码中的注释中标记了此错误所在的行。
列表怎么还没定义?
最佳答案
Firebase CLI 不支持此类函数定义。相反,您应该在函数内部开始初始工作,并稍后缓存结果,这样您就不必再次执行它。或者,您可以尝试开始工作,并保留该函数稍后可以使用的 promise ,如下所示:
const promise = doSomeInitialWork() // returns a promise that resolves with the data
exports.scheduleFreeBed = functions.database.ref(...).onUpdate(change => {
promise.then(results => {
// work with the results of doSomeInitialWork() here
})
})
关于javascript - 如何在 then 语句中定义 firebase 云函数触发器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51527956/
我是一名优秀的程序员,十分优秀!