我正在使用
firebase-admin
来控制使用 Cloud Functions 和 TypeScript 的 firestore 数据库。如果我设置了我的收藏参考的类型,我就不能再使用add
s。 FieldValue
的参数类型需要一个与集合文档匹配的文字对象,没有任何 add
s。在 TypeScript 中使用 FieldValue
s 和 FieldValue
的受支持模式是什么?使用带有 firebase-admin
s 的基础库的任何答案都可以。这是 FieldValue
的最小示例:
FieldValue.serverTimestamp
类似地,与
import { firestore, initializeApp } from 'firebase-admin'
import { firebaseConfig } from './secret'
import { https } from 'firebase-functions'
interface User {
id?: string
displayName: string
createdAt: firestore.Timestamp
}
const converter = {
toFirestore: (data: User) => data,
fromFirestore: (snapshot: firestore.QueryDocumentSnapshot<User>): User => {
const data = snapshot.data()
const doc = { id: snapshot.id, ...data }
return doc
}
}
initializeApp(firebaseConfig)
const db = firestore()
const usersCollectionRef = db.collection('users').withConverter(converter)
export const cloudFunction = https.onCall(async (props, context) => {
const newUserData = {
displayName: 'New User',
createdAt: firestore.FieldValue.serverTimestamp()
}
await usersCollectionRef.add(newUserData)
// Argument of type '{ displayName: string; createdAt: firestore.FieldValue; }' is not assignable to parameter of type 'User'.
// Types of property 'createdAt' are incompatible.
// Type 'FieldValue' is missing the following properties from type 'Timestamp': seconds, nanoseconds, toDate, toMillis
})
:
FieldValue.arrayUnion
interface User {
id?: string
friendNames: string[]
}
const converter = {
toFirestore: (data: User) => data,
fromFirestore: (snapshot: firestore.QueryDocumentSnapshot<User>): User => {
const data = snapshot.data()
const doc = { id: snapshot.id, ...data }
return doc
}
}
initializeApp(firebaseConfig)
const db = firestore()
const usersCollectionRef = db.collection('users').withConverter(converter)
export const cloudFunction = https.onCall(async (props, context) => {
await usersCollectionRef.add({
friendNames: firestore.FieldValue.arrayUnion('BFF')
// Type 'FieldValue' is missing the following properties from type 'string[]': length, pop, push, concat, and 29 more.
})
})
定义为
createdAt
TypeTimestamp
我相信这应该可以解决问题
import type { Timestamp } from 'firebase-admin/firestore';
interface User {
displayName: string
createdAt: Timestamp
}
我想你错过了最后的
import { Timestamp } from 'firebase-admin/firestore'
interface UserType {
id?: string
displayName: string
createdAt: Timestamp
}
....
// creates a firestore hanlder
export const firestore = Object.assign(
() => {
return admin.firestore()
}, {
doc: <T>( path: string ) => {
return admin.firestore()
.doc( path )
.withConverter<T>( converter<T>() )
},
collection: <T>( path: string ) => {
return admin.firestore()
.collection( path )
.withConverter<T>( converter<T>() )
}
} )
// typed converter
const converter = <T>() => ( {
toFirestore: ( data: any ) => data,
fromFirestore: ( doc: any ) => {
return {
...doc.data(),
id: doc.id
} as T
}
} )
// you can now have types while setting and getting data
const usersRef = firestore.collection<UserType>('/users').doc()
await userRef.set({
displayName: 'New User',
createdAt: Timestamp.fromMillis( Date.now() )
}, {
// change/remove this if you don't want merging
merge: true
})
。 使用此 firestore 将为用户创建一个随机 ID 看看
这里