【发布时间】:2023-03-26 14:55:01
【问题描述】:
考虑方法update(documentRef, dataOrField, …preconditionOrValues) → {Transaction}。
我需要能够传递多对 (FieldPath, Value),因为我需要一次更新几个字段。可以这样写:
txn.update(docRef,
'field1', value1,
'field2', value2
);
Typescript 提供 UpdateData 类型:
/**
* Update data (for use with `DocumentReference.update()`) consists of field
* paths (e.g. 'foo' or 'foo.baz') mapped to values. Fields that contain dots
* reference nested fields within the document.
*/
export type UpdateData = {[fieldPath: string]: any};
updateData method signature 被声明为:
/**
* Updates fields in the document referred to by the provided
* `DocumentReference`. The update will fail if applied to a document that
* does not exist.
*
* Nested fields can be updated by providing dot-separated field path
* strings.
*
* @param documentRef A reference to the document to be updated.
* @param data An object containing the fields and values with which to
* update the document.
* @param precondition A Precondition to enforce on this update.
* @return This `Transaction` instance. Used for chaining method calls.
*/
update(documentRef: DocumentReference<any>, data: UpdateData,
precondition?: Precondition): Transaction;
现在,假设我有一堆字段更新,例如:
const fieldUpdates: FirebaseFirestore.UpdateData[] = [
{ ['field1']: value1 },
{ ['field2']: value2 },
];
如何将其传递给update 方法?如下:
txn.update(docRef, ...fieldUpdates);
失败:
Expected at least 2 arguments, but got 1 or more.ts(2557)
firestore.d.ts(378, 49): An argument for 'data' was not provided.
【问题讨论】:
标签: typescript google-cloud-firestore