Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何更新文档引用?_Node.js_Google Cloud Firestore_Firebase Admin - Fatal编程技术网

Node.js 如何更新文档引用?

Node.js 如何更新文档引用?,node.js,google-cloud-firestore,firebase-admin,Node.js,Google Cloud Firestore,Firebase Admin,我试图更新DocumentReference,但无法完成 update()方法失败。如何使用它?(如何传递论点?) firebase管理员版本为6.3.0。 @googlecloud/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 o

我试图更新DocumentReference,但无法完成

update()
方法失败。如何使用它?(如何传递论点?)

firebase管理员
版本为6.3.0。
@googlecloud/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)

按照您发布的示例,您正在尝试更新一个不存在的文档。在这种情况下,您应该首先创建它

如果不确定文档是否存在,请传递将新数据与任何现有文档合并的选项,以避免覆盖整个文档

从谷歌的文档中:

var cityRef = db.collection('cities').doc('BJ');

var setWithOptions = cityRef.set({
  capital: true
}, {merge: true});

参考资料:

我从未像您在示例中那样尝试在functions:shell中使用Admin SDK。然而,我可以很容易地重现你的错误。我猜函数:shell以某种方式篡改了变量
更新
。当我记录
更新时
未定义在输出中串联

firebase > console.log(updates)
{ email2: 'xxx@yyy.zzz' }
undefined
如果你把你的代码放在一个JS文件中,用一个服务帐户初始化你的应用程序,然后运行在node中,它很可能工作得很好


首先创建一个对象,然后将值分配给该对象上的字段/属性,如:

var myObj = {};
myObj["myfield"] = myvalue;

现在作为
.set
.update

的第二个参数传递,我很抱歉缺少信息。文档已经创建,所以
get()
可以工作。我在我的帖子中添加了更多的信息。你能解决这个问题吗?