Firebase: 尝试返回服务器时间戳时出现命名空间错误

huangapple go评论68阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年4月4日 09:29:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75924843.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定