Firebase-functions: Unable to write user record to firestore

Created on 12 Jan 2018  路  14Comments  路  Source: firebase/firebase-functions

If i use user.json in on-user-created trigger it gives following error

Error: Argument "data" is not a valid Document. Input is not a plain JavaScript object. at Object.exports.(anonymous function) [as isDocument] (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/src/validate.js:86:15) at WriteBatch.set (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/src/write-batch.js:264:14) at DocumentReference.set (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/src/reference.js:416:8) at exports.onUsercreated.functions.auth.user.onCreate.event (/user_code/index.js:125:17) at Object. (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:59:27) at next (native) at /user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71 at __awaiter (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12) at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:53:36) at /var/tmp/worker/worker.js:695:26

If i use user.toJSON() it says method undefined

Most helpful comment

was getting this same error... Error: Argument "data" is not a valid Document. Input is not a plain JavaScript object... did this to it... queryAsObject = JSON.parse(JSON.stringify(queryAsObject))

and it worked. so who knows...

All 14 comments

@suredoctorofficial This was likely fixed in the latest release. Can you update both firebase-functions and firebase-admin to the latest version? And if you still see the issue, please file another issue and provide code sample

"dependencies": {
"firebase": "^4.9.1",
"firebase-admin": "^5.8.2"
}

Error: Argument "data" is not a valid Document. Input is not a plain JavaScript object

@MrGoogol you don't have the right dependencies, you should not need to use "firebase" at all, and you should use the latest "firebase-functions"

I am seeing this problem as well. These are my dependencies:

"@google-cloud/firestore": "^0.13.0",
"firebase-admin": "^5.10.0",
"firebase-functions": "^0.9.0",

@Binghammer Can you file a separate issue with a full repro?

was getting this same error... Error: Argument "data" is not a valid Document. Input is not a plain JavaScript object... did this to it... queryAsObject = JSON.parse(JSON.stringify(queryAsObject))

and it worked. so who knows...

@blexo cc. @laurenzlong

TL;DR It's not firebase's issue, just need a prototype of Object.

Though this issue has been closed for a while, I might find a potential cause of this.
What @blexo mention works it's because you might send something without prototype of Object,
so this:

// before { key: 'value' }
queryAsObject = JSON.parse(JSON.stringify(queryAsObject))
// after: { key: 'value', __proto__: Object}

Might fix this issue, but you can also use Object.assign({}, queryAsObject) can also do the trick.
The real issue is why your input loses prototype, it's an interesting issue, but beyond the scope of firebase I think.

I do not understand the proposed "solution."

I am facing this same issue. I am not sure why this problem has arisen for me following only minor modifications to an index.js file that previously worked.

Please can someone give a clear explanation of how to resolve the issue.

I'm also having this issue with versions:

{ 
  "firebase-admin": "6.0.0",
  "firebase-functions": "^2.0.5"
}

tried all the workarounds mentioned above, parsing and stringifying, and vice versa, assigning it to a new object and others all with the same error message:

Error: Argument "data" is not a valid Document. Input is not a plain JavaScript object.
    at Validator.(anonymous function).values [as isDocument] (~/functions/node_modules/@google-cloud/firestore/build/src/validate.js:99:27)

I'm also having same issue, anybody found solution, please share.

Hey everyone, firebase-functions uses the Firestore Node SDK under the hood. I found this issue that sounds similar: https://github.com/googleapis/nodejs-firestore/issues/213. Please comment on the issue with the data that you are trying to write to Firestore (ideally code samples) so that it can be diagnosed.

The message is not accurate either, just modified a function after months and now i'm hitting this bug.

The code

exports.createUser = functions.auth.user().onCreate(user => {
  const { displayName, email, photoURL, uid } = user;

  const newUser = {
      name: displayName,
      photoURL,
      email,
      active: false,
      complete: false,
  };

  console.log('Writing new user to database (uid, data)', uid, newUser);

  return firestore
    .collection('users')
    .add(uid)
    .set(newUser);
});

Edit: nvm seems my usage was wrong, I was calling .add then .set for whatever reason. Changing it to .doc then .set worked for me.

Hey everyone, I found a similar issue with my project. When I was making a post request from the frontend side, it gave me the error: "Value for argument "data" is not a valid Firestore document. Cannot use "undefined" as a Firestore value (found in field name)"
The issue was resolved for me just by specifying the content-type as application/json in the header object of my PUSH request.
I hope this helps.

we were versioning our content type header and it ended up causing this issue. Would really like it if firebase would accept versioned content type headers in the future.

Was this page helpful?
0 / 5 - 0 ratings