如何使用AngularFire在Firebase中保存DATE字段

编程入门 行业动态 更新时间:2024-10-26 19:37:36
本文介绍了如何使用AngularFire在Firebase中保存DATE字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个DATE字段(开始日期),用户可以输入任何日期的屏幕

I have a screen with a DATE field (Start Date) where the user can enter any date

<label class="item item-input"> <span class="input-label">Start Date</span> <input type="date" ng-model="currentItem.OpenDate"> </label>

我在保存按钮的点击事件中添加了以下内容:

I added the following to the Save button's click event

console.log("Normal date " + $scope.currentItem.OpenDate);

控制台显示以下日期

The console shows the following date

Normal date Fri May 01 2015 00:00:00 GMT-0400 (Eastern Daylight Time)

以下是推送事件

Here's the push event

$scope.data.accounts.push({ 'AccountName': $scope.currentItem.AccountName, 'StartBalance': $scope.currentItem.StartBalance, 'OpenDate': $scope.currentItem.OpenDate, 'AccountType': $scope.currentItem.AccountType });

然而,日期 $ scope.currentItem.OpenDate 没有保存到Firebase,其余数据正常保存。我错过了什么?

HOWEVER, the date $scope.currentItem.OpenDate is not getting saved to Firebase, the rest of the data is saving properly. What am I missing?

推荐答案

你不幸的是没有包含初始化 OpenDate 属性。但看起来您正尝试将JavaScript Date 对象写入Firebase。 Firebase文档指定它支持以下类型:

You unfortunately didn't include the code that initializes the OpenDate property. But it looks like you're trying to write a JavaScript Date object into Firebase. The Firebase documentation specifies that it supports these types:

对象,数组,字符串,数字,布尔值或 null

为了存储 Date 的值,您必须将其转换为受支持的类型。例如

In order to store the Date's value, you will have to convert it to a supported type. E.g.

$scope.data.accounts.push({ 'AccountName': $scope.currentItem.AccountName, 'StartBalance': $scope.currentItem.StartBalance, 'OpenDate': $scope.currentItem.OpenDate.toString(), 'AccountType': $scope.currentItem.AccountType });

或者:

Or alternatively:

'OpenDate': $scope.currentItem.OpenDate.getTime(),

更多推荐

如何使用AngularFire在Firebase中保存DATE字段

本文发布于:2023-10-24 02:57:52,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   如何使用   Firebase   AngularFire   DATE

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!