问题描述
我的计划的云函数每隔2分钟从一个Firestore集合中读取数据,该集合的数据少于1 MB,并且仅将“函数”和“ admin”作为全局变量,但是却出现了内存限制超出的错误。我想知道(1)是什么原因引起的,(2)如何清除内存。
一些线索
- 最初,我在云函数的末尾没有
return true
,只有一个“函数返回未定义”错误。但是,我没有收到“超出内存限制”错误,并且该功能按预期工作。在firestore更新方法中(在函数末尾附近)添加了2个字段后,报告了“超出内存限制”错误,并且该函数停止工作。 - 现在,仅在控制台上打印“开始”,然后显示“超出了内存限制。功能调用被中断。”并且代码没有进入get()的回调。
index.js
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.scheduledFunction = functions.pubsub.schedule('every 2 minutes').onRun((context) => {
var rooms = [];
var open;
console.log("start");
const db = admin.firestore();
return db.collection("rooms").get().
then((query) => {
console.log(query.docs);
rooms = query.docs;
for (var i = 0; i < rooms.length; i++) {
open = rooms[i].data().open;
const temp = rooms[i].data();
const roomid = rooms[i].id;
console.log(temp);
console.log(roomid);
console.log(typeof temp);
if (temp.hasOwnProperty('schedule')) {
const schedule = temp.schedule;
const scheduleDuplicate = schedule;
console.log(schedule);
//The nested while loops below basically check whether a scheduled virtual room should be displayed by comparing current time with the scheduled time periods
var j = 0;
var check = false;
while (j < schedule.length && check === false) {
const slots = schedule[j].slots;
var z = 0;
while (z < slots.length && check === false) {
const array = slots[z].array;
var k = 0;
while (k < array.length && check === false) {
const currentDate = new Date().toUTCString();
const currentTime = new Date().getTime();
const startTime = new Date(array[k].startTime).getTime();
const endTime = new Date(array[k].endTime).getTime();
console.log(array[k].startTime,startTime);
console.log(array[k].endTime,endTime);
console.log(currentDate,currentTime);
if (currentTime > endTime) {
console.log("too late");
array.splice(k,1);
}
else if (currentTime >= startTime && currentTime <= endTime) {
console.log("between");
check = true;
break;
}
else if (currentTime < startTime) {
console.log("too early");
break;
}
}
console.log(array);
if (array.length === 0) {
slots.splice(z,1);
}
else slots[z].array = array;
}
console.log(slots);
if (slots.length === 0) {
schedule.splice(j,1);
}
else schedule[j].slots = slots;
}
console.log(schedule);
return db.runTransaction((transaction) => {
return transaction.get(db.collection("rooms").doc(roomid))
.then((doc) => {
if (doc.exists) {
transaction.update(db.collection("rooms").doc(roomid),{schedule: schedule});
return "dummy";
}
})
})
.then((dummy) => {
if (check === false && open === true) {
console.log("close the room");
db.collection("rooms").doc(roomid).update({
open: false
})
.then((dummy) => {
return true;
})
}
else if (check === true && open === false) {
console.log("open the room");
db.collection("rooms").doc(roomid).update({
//Added "notifications" and "participants" fields to be updated,deployed the function again,and the "memory-limit-exceeded" error" occuered.
notifications: 0,participants: [],open: true
})
.then((dummy) => {
return true;
})
}
return true;
})
}
}
return true;
})
.then((dummy) => {
return true;
})
})
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)