简体   繁体   English

如何使用 angular 从 firebase 获取服务器时间?

[英]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.

相关问题 如何使用 (.get) 从 firebase 读取数据(一次读取) - how can i read data from firebase (one time read ) using (.get) 如何使用 Flutter 仅从 firebase 实时数据库中的给定数字获取时间范围? - How can I get only the time range from the given figure in firebase realtime database using Flutter? 我无法从他给我 [object Object] 使用 angular 和 firebase 的客户那里获取详细信息 - I can't get details from a client he gives me [object Object] using angular and firebase 我可以从 firebase 实时数据库中获取报告吗? - Can I get reports from firebase real time database? 我如何使用一种方法从 Firebase 实时数据库中获取单个值 - How can i fetch single value from Firebase real time database using a method 如何使用 window 服务自动获取 firebase 数据 - How can i get firebase data automatically using window service 如何从托管URL的firebase与API服务器通信? - How can I communicate with the API server from the firebase hosting URL? 如何从 Firebase Firestore 获取 ReactJs 的数据? - How Can I get data with ReactJs from Firebase Firestore? 我如何不重复地从firebase数据库中获取数据 - how I can get data from the firebase database without repeat 如何使用 NextJs API、Firebase Firestore、axios 和 TypeScript 从 Firebase 集合中获取数据? - How do I get data from Firebase collection using NextJs API, Firebase Firestore, axios and TypeScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM