如何在firestore

时间:2018-02-13 00:06:02

标签: angular google-cloud-firestore

html中的

type="date"在firestore中保存为字符串。

在html文件中。

<input #scheduledStartDate="ngModel" [(ngModel)]="reposition.scheduledStartDate"
name="scheduledStartDate" id="scheduledStartDate" class="input is-normal"
type="date" required placeholder="Leaving...">

来自界面.ts文件

scheduledStartDate?: DateTimeFormat;

也试过

scheduledStartDate?: Date;

结果相同。

两个选项都将输入的日期值保存为firestore中的字符串,例如&#34; 2018年1月2日&#34;而不是时间戳。

2 个答案:

答案 0 :(得分:3)

Before saving the javascript object to firestore, just create an instance of Date() and pass the value like this new Date("December 10, 1815")

Syntax

var docData = {
    stringExample: "Hello world!",
    booleanExample: true,
    numberExample: 3.14159265,
    dateExample: new Date("December 10, 1815"),
    arrayExample: [5, true, "hello"],
    nullExample: null,
    objectExample: {
        a: 5,
        b: {
            nested: "foo"
        }
    }
};
db.collection("data").doc("one").set(docData).then(function() {
    console.log("Document successfully written!");
})

Cloud Firestore supports,

Date and time - Chronological - When stored in Cloud Firestore, precise only to microseconds; any additional precision is rounded down.

Note

When a query involves a field with values of mixed types, Cloud Firestore uses a deterministic ordering based on the internal representations.

Official Links

https://firebase.google.com/docs/firestore/manage-data/data-types https://firebase.google.com/docs/firestore/manage-data/add-data

答案 1 :(得分:0)

您应按以下方式使用Firestore函数firebase.firestore.Timestamp.fromDate

const mydoc = {
  name: 'John Doe',
  createdAt: firebase.firestore.Timestamp.fromDate(new Date("December 10, 1815")) 
}

// save the document 
db.collection("mycollection").doc("id1").set(mydoc).then(function() {
    console.log("Document successfully written!");
});