如何使用AngularFire在Firebase中保存DATE字段 [英] How do you save a DATE field in Firebase using AngularFire

查看:144
本文介绍了如何使用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字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆