Typescript 如何使用强类型UpdateData类型调用Transaction.update
考虑一下这个方法 我需要能够传递多对(FieldPath,Value),因为我需要一次更新几个字段。可以这样写:Typescript 如何使用强类型UpdateData类型调用Transaction.update,typescript,google-cloud-firestore,Typescript,Google Cloud Firestore,考虑一下这个方法 我需要能够传递多对(FieldPath,Value),因为我需要一次更新几个字段。可以这样写: txn.update(docRef, 'field1', value1, 'field2', value2 ); Typescript提供以下类型: 声明如下: /** * Updates fields in the document referred to by the provided * `DocumentReference`. The u
txn.update(docRef,
'field1', value1,
'field2', value2
);
Typescript提供以下类型:
声明如下:
/**
* 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;
我如何将此信息传递给更新方法?
以下信息:
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.
只需传递一个对象,其中包含要更新的字段的名称和值
txn.update(docRef, {
field1: value1,
field2: value2
})
在TypeScript中,
{[fieldPath:string]:any}
表示“具有属性的对象必须是字符串,其值可以是任何内容”。在本例中,这些键是要更新的字段的路径。只需传递一个对象,其中包含要更新的字段的名称和值
txn.update(docRef, {
field1: value1,
field2: value2
})
在TypeScript中,
{[fieldPath:string]:any}
表示“具有属性的对象必须是字符串,其值可以是任何内容”。在本例中,这些键是要更新的字段的路径。我需要强调的是,我的fieldUpdates
实际上是一个动态更新列表。换句话说,我并没有像你所描述的那样传递一个常量结构。然后动态地构建你的更新对象。API不关心您如何制作它。你在这里和文档中看到的只是为了简单起见,来说明API是如何工作的。我刚刚明白了!现在我让它工作了。感谢您的澄清。我需要强调的是,我的fieldUpdates
实际上是一个动态更新列表。换句话说,我并没有像你所描述的那样传递一个常量结构。然后动态地构建你的更新对象。API不关心您如何制作它。你在这里和文档中看到的只是为了简单起见,来说明API是如何工作的。我刚刚明白了!现在我让它工作了。谢谢你的澄清。
txn.update(docRef, {
field1: value1,
field2: value2
})