6

I am trying to create a doc with a reference on Firestore with NodeJS v8.6.0. Like this

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 })

Although ref is a DocumentReference, it results

Error: Argument "data" is not a valid Document. Input object is deeper than 20 levels or contains a cycle.

Is there anyway to do it on NodeJS (Admin)SDK?


In addition, the package used in the code is

"firebase-admin": "^5.4.1"

and

console.log(ref)

outputs

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)' } }

and the full error message is

/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)
8
  • @NovoLucas Seems that the sample doesn't save any reference. Can you indicate where it is? Commented Oct 4, 2017 at 13:32
  • This should work so this could be a dependency issue such that DocumentReference fails the instanceof check in set. How are you importing firestore? Are you sure you're only importing it once? Could you console.log(ref); Also, which version of node are you using? Commented Oct 4, 2017 at 21:00
  • @GilGilbert edited the post. Please check. And, yes, I can reproduce it with minimum code with NodeJS v8.6.0. "require('firebase-admin')" once, "admin.initializeApp" once. Commented Oct 5, 2017 at 3:43
  • 1
    Thanks. We've been able to reproduce this. It's a bug in an interaction between the admin SDK and the regular node SDK. This works via the regular node.js SDK that you can get as described here: cloud.google.com/nodejs/docs/reference/firestore/0.8.x Commented Oct 5, 2017 at 4:17
  • 1
    Thanks Gil. Looking forward to the patch : ) Commented Oct 5, 2017 at 4:25

2 Answers 2

7

This was a bug in an interaction between the admin SDK and the regular node SDK for Firestore.

An update to @google-cloud/firestore 0.8.2 should fix this issue.

You can update your project with npm update to get this change.

Sign up to request clarification or add additional context in comments.

Comments

1

I encountered a similar error when implementing background functions to handle document update events. As the docs suggest, it is important to avoid infinite loops by checking that you do not inadvertently end up re-triggering the same function:

Any time you write to the same document that triggered a function, you are at risk of creating an infinite loop. Use caution and ensure that you safely exit the function when no change is needed

And also from here:

Caution: Be careful to avoid any situation in which the function's result actually retriggers the function, creating an infinite loop — for example, a function triggered by writes to a specific Cloud Firestore document that terminates by writing to that same path. Also make sure to write functions in an idempotent way, so that they produce the same result if they run multiple times for a single event.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.