我尝试更新 DocumentReference 但无法执行此操作。
update()
方法失败。如何使用它? (如何传递参数?)
firebase-admin
版本为 6.3.0。
@google-cloud/firestore
版本为 0.19.0。
❯ firebase functions:shell
i functions: Preparing to emulate functions.
Warning: You're using Node.js v8.14.0 but Google Cloud Functions only supports v6.11.5.
✔ functions: sampleFunc
firebase > const admin = require('firebase-admin');
firebase > admin.initializeApp();
firebase > let ref = admin.firestore().collection("users").doc('edqupYQhzqV1ODjEpoJn');
firebase > let updates = { email: 'xxx@yyy.zzz' };
firebase > ref.update(updates).then(value => console.log(value) );
Error: Update() requires either a single JavaScript object or an alternating list of field/value pairs that can be followed by an optional precondition. Argument "dataOrField" is not a valid Document. Input is not a plain JavaScript object.
at WriteBatch.update (/Users/xxx/Desktop/sample-functions/functions/node_modules/@google-cloud/firestore/build/src/write-batch.js:359:23)
at DocumentReference.update (/Users/xxx/Desktop/sample-functions/functions/node_modules/@google-cloud/firestore/build/src/reference.js:387:14)
更新
文档已经创建,因此 get()
可以工作。
firebase > ref.get().then(snapshot => console.log(snapshot.data()));
set()
中也会出现错误。
firebase > ref.set({email: 'aaa@bbb.ccc'}, {merge: true}).then(value => console.log(value));
Error: Argument "data" is not a valid Document. Input is not a plain JavaScript object.
at Validator.(anonymous function).values [as isDocument] (/Users/xxx/Desktop/sample-functions/functions/node_modules/@google-cloud/firestore/build/src/validate.js:99:27)
at WriteBatch.set (/Users/xxx/Desktop/sample-functions/functions/node_modules/@google-cloud/firestore/build/src/write-batch.js:232:25)
at DocumentReference.set (/Users/xxx/Desktop/sample-functions/functions/node_modules/@google-cloud/firestore/build/src/reference.js:349:27)
按照您发布的示例,您正在尝试更新不存在的文档。在这种情况下,您应该首先创建它。
如果您不确定文档是否存在,请传递将新数据与任何现有文档合并的选项,以避免覆盖整个文档。
来自 Google 的文档:
var cityRef = db.collection('cities').doc('BJ');
var setWithOptions = cityRef.set({
capital: true
}, {merge: true});
引用:Firestore Add Data
我是一名优秀的程序员,十分优秀!