事务.getAll方法不能用作documented。我已复制并粘贴给定示例中的代码,但失败并显示错误。
从Firestore检索多个文档。对所有返回的文档持有悲观锁定。
(node:24009) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: transaction.getAll is not a function
这直接从文档中复制并添加了初始化标题。
const firebase = require('firebase-admin');
var serviceAccount = process.env.MY_CREDENTIALS;
firebase.initializeApp({
credential: firebase.credential.cert(JSON.parse(serviceAccount))
});
const firestore = firebase.firestore();
let firstDoc = firestore.doc('col/doc1');
let secondDoc = firestore.doc('col/doc2');
let resultDoc = firestore.doc('col/doc2');
firestore.runTransaction(transaction => {
return transaction.getAll(firstDoc, secondDoc).then(docs => {
transaction.set(resultDoc, {
sum: docs[1].get('count') + docs[2].get('count')
});
});
});
这支持Firestore Node.js客户端的v12,其中包括transaction.getAll的提交
我从package.json中删除了node_modules目录和所有依赖项,然后运行以下命令...
$ npm install --save [email protected]
> [email protected] install /home/jason/Downloads/projects/testing/node_modules/grpc
> node-pre-gyp install --fallback-to-build --library=static_library
[grpc] Success: "/home/jason/Downloads/projects/testing/node_modules/grpc/src/node/extension_binary/node-v57-linux-x64-glibc/grpc_node.node" is installed via remote
> [email protected] postinstall /home/jason/Downloads/projects/testing/node_modules/google-gax/node_modules/protobufjs
> node scripts/postinstall
> [email protected] postinstall /home/jason/Downloads/projects/testing/node_modules/google-proto-files/node_modules/protobufjs
> node scripts/postinstall
npm WARN [email protected] No description
npm WARN [email protected] No repository field.
+ [email protected]
added 358 packages in 42.265s
(xenial)jason@localhost:~/Downloads/projects/testing$ node testTransactionGetAll.js
(node:22273) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: Cannot read property 'get' of undefined
(node:22273) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
transaction.getAll
已添加到Google Cloud Firestore Node.js Client的v0.12.x此更新已添加到Firebase Admin Node.js SDK v5.9.1
但是,文档中的示例代码不正确。以下行应更改为:
sum: docs[0].get('count') + docs[1].get('count')
以前,它引用了doc[2]
,它不存在