当用户登录我的应用程序时,我想将初始详细信息(如用户联系人列表)发送到后端以处理用户创建。由于这是一项很长的工作,我想分成单独的HTTPs调用。
为简单起见,这是应用程序端请求:
exports.sandbox = functions.https.onCall((data, context) => {
console.log("In, data.val is " + data.val);
});
和HTTPs函数(在Node.js中):
firebase functions:shell
如果我通过for (let i = 0; i < 3; ++i) {
sandbox.post('').json({"data": {"val": "" + i}});
}
shell调用该函数,如下所示:
In, data.val is 0
In, data.val is 1
In, data.val is 2
我得到了输出:
2018-05-16T10:09:02.080247334Z D sandbox: Function execution started
2018-05-16T10:09:02.080333313Z D sandbox: Billing account not configured.
External network is not accessible and quotas are severely limited.
Configure billing account to remove these restrictions
2018-05-16T10:09:03.051Z I sandbox: In, data.val is 2
2018-05-16T10:09:03.135023272Z D sandbox: Function execution took 1056 ms, finished with status code: 200
2018-05-16T10:09:04.073804790Z D sandbox: Function execution started
2018-05-16T10:09:04.073930247Z D sandbox: Billing account not configured.
External network is not accessible and quotas are severely limited.
Configure billing account to remove these restrictions
2018-05-16T10:09:04.236Z I sandbox: In, data.val is 2
2018-05-16T10:09:04.239244077Z D sandbox: Function execution took 166 ms, finished with status code: 200
2018-05-16T10:09:06.722270621Z D sandbox: Function execution started
2018-05-16T10:09:06.722387172Z D sandbox: Billing account not configured.
External network is not accessible and quotas are severely limited.
Configure billing account to remove these restrictions
2018-05-16T10:09:09.963Z I sandbox: In, data.val is 2
2018-05-16T10:09:09.971406186Z D sandbox: Function execution took 3250 ms, finished with status code: 200
但是,如果应用代码运行,Firebase控制台会显示以下日志:
In, data.val is 2
我看了{{1}}三次。发生了什么事?
答案 0 :(得分:1)
请记住firebaseFunctions.getHttpsCallable("sandbox").call(data)
是异步的,这意味着它会立即返回。您的代码将快速连续启动三个电话。
另请注意,他们都共享相同的data
对象。这里可能发生的是,数据的“最终”静止值包含值为2的val键。这很可能是三个调用中的每个调用最终都会发送到函数,因为循环将完成执行在每个请求发出之前。
不是在循环的每次迭代中修改相同的data
,而是创建 new 数据对象,并使用每个请求所需的所有数据填充它(不要重复使用请求之间的数据对象。)