英文:
Firebase: Trying to return the server timestamp gives a namespace error
问题
以下是翻译好的部分:
不确定我做错了什么。错误指向返回类型。我试图小心地添加太多命名空间,因为我不想减慢我的函数的冷启动,但显然我在这里漏掉了一些关键内容...
import * as functions from "firebase-functions";
import * as admin from "firebase-admin";
admin.initializeApp();
const fs = admin.firestore();
/**
* 从Firestore返回服务器时间戳。
*
* @returns {Timestamp} 服务器时间戳。
*/
export function getServerTimestamp(): firebase.firestore.Timestamp {
// return firebase.firestore.FieldValue.serverTimestamp();
return FirebaseFirestore.FieldValue.serverTimestamp();
}
英文:
Not sure what I'm doing wrong. The error is pointing to the return type. I'm trying to be careful with adding too many namespaces because I don't want to slow down the cold starts of my functions, but obviously I'm missing something crucial here...
import * as functions from "firebase-functions";
import * as admin from "firebase-admin";
admin.initializeApp();
const fs = admin.firestore();
/**
* Returns the server timestamp from Firestore.
*
* @returns {Timestamp} The server timestamp.
*/
export function getServerTimestamp(): firebase.firestore.Timestamp {
// return firebase.firestore.FieldValue.serverTimestamp();
return FirebaseFirestore.FieldValue.serverTimestamp();
}
答案1
得分: 2
You can use admin.firestore.Timestamp
or Timestamp
from import {Timestamp} from "firebase-admin/firestore";
to type the return type of your function and to return the Firestore server Timestamp you can use either of the following snippet:
import * as admin from "firebase-admin";
admin.initializeApp();
/**
* Returns the server timestamp from Firestore.
* @return {Timestamp} The server timestamp.
*/
export function getServerTimestamp(): admin.firestore.Timestamp {
// return firebase.firestore.FieldValue.serverTimestamp();
return admin.firestore.Timestamp.now();
}
OR
import * as admin from "firebase-admin";
import {Timestamp} from "firebase-admin/firestore";
admin.initializeApp();
/**
* Returns the server timestamp from Firestore.
* @return {Timestamp} The server timestamp.
*/
export function getServerTimestamp(): Timestamp {
// return firebase.firestore.FieldValue.serverTimestamp();
return admin.firestore.Timestamp.now();
}
Reference: Timestamp
英文:
You can use admin.firestore.Timestamp
or Timestamp
from
import {Timestamp} from "firebase-admin/firestore";
to type the return type of your function and to return the Firestore server Timestamp you can use either of the following snippet :
import * as admin from "firebase-admin";
admin.initializeApp();
/**
* Returns the server timestamp from Firestore.
* @return {Timestamp} The server timestamp.
*/
export function getServerTimestamp(): admin.firestore.Timestamp {
// return firebase.firestore.FieldValue.serverTimestamp();
return admin.firestore.Timestamp.now();
}
OR
import * as admin from "firebase-admin";
import {Timestamp} from "firebase-admin/firestore";
admin.initializeApp();
/**
* Returns the server timestamp from Firestore.
* @return {Timestamp} The server timestamp.
*/
export function getServerTimestamp(): Timestamp {
// return firebase.firestore.FieldValue.serverTimestamp();
return admin.firestore.Timestamp.now();
}
Reference : Timestamp
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论