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

查看:22
本文介绍了如何使用 AngularFire 在 Firebase 中保存 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);

控制台显示以下日期

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

这里是推送事件

$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

object, array, string, number, boolean, or 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 
});

或者:

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

这篇关于如何使用 AngularFire 在 Firebase 中保存 DATE 字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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