[英]How can i get server time from firebase using angular?
I am working with firebase and angular application.我正在使用 firebase 和 angular 应用程序。 I was working with
new Date()
, but now our requirement is to change this function to server time.我正在使用
new Date()
,但现在我们的要求是将此 function 更改为服务器时间。 But I have no idea how can i get server time from firebase.但我不知道如何从 firebase 获取服务器时间。
Code代码
I save time in firebase like this.我这样在 firebase 中节省时间。
this.projectForm.value.createdAt = firebase.database.ServerValue.TIMESTAMP;
this.firestore.collection('projects').add(this.projectForm.value);
But no idea how can I get the value但不知道如何获得价值
this.firestore.collection('projects').doc(this.projectId).get().subscribe(snapshot => {
const project: any = snapshot.data();
});
Any solution appreciated!任何解决方案表示赞赏!
I hope I'm not missing out on something here, I'm not sure what exactly the problem is.我希望我没有错过这里的某些东西,我不确定到底是什么问题。 What exactly are you trying to do with the project?
你到底想用这个项目做什么?
You are declaring a local variable in the您正在声明一个局部变量
this.firestore.collection('projects').doc(this.projectId).get().subscribe(snapshot => {
const project: any = snapshot.data();
});
snippet.片段。
Therefore you can only access the project in this codeblock.因此,您只能访问此代码块中的项目。
Try尝试
this.firestore.collection('projects').doc(this.projectId).get().subscribe(snapshot => {
const project: any = snapshot.data();
console.log('CreatedAt', project.createdAt);
});
And check the console for output.并检查 output 的控制台。
PS: You are using TypeScript you should define a type for Project
. PS:您正在使用 TypeScript 您应该为
Project
定义一个类型。 Using any
type is very bad practice.使用
any
类型都是非常糟糕的做法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.