在Firestore上的Document中设置DocumentReference(NodeJS)

时间:2017-10-04 12:44:01

标签: node.js firebase google-cloud-firestore

我正在尝试使用NodeJS v8.6.0创建一个带有Firestore参考的文档。喜欢这个

const admin = require('firebase-admin')
admin.initializeApp({ credential: MY_CREDENTIAL, databaseURL: MY_DB_URL })
const db = admin.firestore()

const ref = db.doc('foo/someDoc')
db.doc('bar/targetDoc').set({ link: ref })

虽然refDocumentReference,但结果为

  

错误:参数“data”不是有效的Document。输入对象更深   超过20个级别或包含一个周期。

无论如何在NodeJS(Admin)SDK上执行此操作吗?

此外,代码中使用的包是

"firebase-admin": "^5.4.1"

console.log(ref)

输出

DocumentReference {
  _firestore: 
   Firestore {
     makeAuthenticatedRequest: 
      { [Function: makeAuthenticatedRequest]
        getCredentials: [Function: bound getCredentials],
        authClient: [Object] },
     authClient: 
      Auth {
        authClientPromise: null,
        authClient: null,
        config: [Object],
        environment: {} },
     baseUrl: undefined,
     getCredentials: [Function: bound getCredentials],
     globalInterceptors: [],
     interceptors: [],
     packageJson: 
      { name: '@google-cloud/firestore',
        version: '0.8.1',
        author: 'Google Inc.',
        description: 'Firestore Client Library for Node.js',
        contributors: [Array],
        main: './src/index.js',
        files: [Array],
        repository: 'googleapis/nodejs-firestore',
        keywords: [Array],
        dependencies: [Object],
        devDependencies: [Object],
        scripts: [Object],
        license: 'Apache-2.0',
        engines: [Object],
        types: './types/firestore.d.ts' },
     projectId: 'MY_PROJECT',
     projectIdRequired: true,
     Promise: [Function: Promise],
     grpcMetadata: Metadata { _internal_repr: [Object] },
     maxRetries: undefined,
     userAgent: 'gcloud-node-firestore/0.8.1',
     activeServiceMap_: Map {},
     protos: {},
     _preferTransactions: false,
     _lastSuccessfulRequest: null,
     api: { Firestore: [Object] },
     _referencePath: 
      ResourcePath {
        segments: [],
        _formattedName: undefined,
        _projectId: 'dewpod-dev',
        _databaseId: '(default)' },
     app: 
      FirebaseApp {
        firebaseInternals_: [Object],
        services_: [Object],
        isDeleted_: false,
        name_: '[DEFAULT]',
        options_: [Object],
        database: [Function: bound ],
        auth: [Function: bound ],
        messaging: [Function: bound ],
        storage: [Function: bound ],
        firestore: [Function: bound ],
        INTERNAL: [Object] },
     INTERNAL: FirestoreInternals {} },
  _referencePath: 
   ResourcePath {
     segments: [ 'orgs', 'fooOrg' ],
     _formattedName: undefined,
     _projectId: 'dewpod-dev',
     _databaseId: '(default)' } }

,完整的错误消息是

/SOME_PATH/node_modules/@google-cloud/firestore/src/validate.js:86
        throw new Error(message);
        ^

Error: Argument "data" is not a valid Document. Input object is deeper than 20 levels or contains a cycle.
    at Object.exports.(anonymous function) [as isDocument] (/SOME_PATH/node_modules/@google-cloud/firestore/src/validate.js:86:15)
    at WriteBatch.set (/SOME_PATH/node_modules/@google-cloud/firestore/src/write-batch.js:251:14)
    at DocumentReference.set (/SOME_PATH/node_modules/@google-cloud/firestore/src/reference.js:416:8)
    at Object.<anonymous> (/SOME_PATH/fstest.js:17:20)
    at Module._compile (module.js:624:30)
    at Object.Module._extensions..js (module.js:635:10)
    at Module.load (module.js:545:32)
    at tryModuleLoad (module.js:508:12)
    at Function.Module._load (module.js:500:3)
    at Function.Module.runMain (module.js:665:10)

2 个答案:

答案 0 :(得分:7)

这是管理SDK与常规节点SDK for Firestore之间交互的错误。

@ google-cloud / firestore 0.8.2的更新应解决此问题。

您可以使用npm update更新项目以获得此更改。

答案 1 :(得分:0)

在实现后台功能以处理文档更新事件时遇到了类似的错误。与docs suggest一样,重要的是要避免无限循环,方法是检查您不会无意间导致重新触发同一函数:

每当您写入触发功能的同一文档时,就有创建无限循环的风险。请谨慎操作,并确保在不需要更改时安全退出该功能

并且也来自here

警告:小心避免在函数结果实际触发函数的任何情况下创建无限循环,例如,写入特定Cloud Firestore文档触发的函数会因写入同一路径而终止。另外,请确保以幂等的方式编写函数,这样,如果针对单个事件多次运行它们,它们将产生相同的结果。