【发布时间】:2021-03-17 01:46:00
【问题描述】:
通过将 DateTime 对象从应用程序直接发送到 Firebase,会自动完成转换为 TimeStamp 格式的操作。但是,如果我通过 Cloud Function 发送相同的对象,则会收到以下错误:
Invalid argument: Instance of 'DateTime'
我尝试使用 toString() 方法传递对象并通过运行 Date.parse Date.parse(data["birth_date"]) 进行后端转换。但在 Firestore 上,数据记录为数字而不是时间戳。
颤振代码:
//final DateTime birthDate; -> To show you the type
final HttpsCallable callable =
CloudFunctions(region: "europe-west3").getHttpsCallable(
functionName: 'userFunctions-editUser',
);
HttpsCallableResult httpsCallableResult =
await callable.call({"birth_date": birthDate});
服务器端代码:
exports.addUser = functions
.region("europe-west3")
.https.onCall((data, context) => {
const userUid = data["userUid"];
const docRef = admin.firestore().doc(`/users_profils/${userUid}`);
return docRef.set({
...
birth_date: Date.parse(data["birth_date"]),
...
});
});
如何使用云函数将 DateTime 对象转换为时间戳?
【问题讨论】:
-
请编辑问题以显示在客户端和服务器端都没有按您期望的方式工作的代码。我们看不出您是否做错了什么。
标签: firebase flutter google-cloud-firestore google-cloud-functions