Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cloud Firestore node.js invalid use of type “object” as a Firestore argument #618

Closed
kiwicopple opened this issue Aug 8, 2019 · 3 comments
Assignees

Comments

@kiwicopple
Copy link

[REQUIRED] Step 2: Describe your environment

  • Operating System version: MacOS
  • Firebase SDK version: 8.3.0
  • Library version: _____
  • Firebase Product: Firestore database
  • Node Version: 10.15.1

[REQUIRED] Step 3: Describe the problem

Steps to reproduce:

This issue (by someone else) describes both the problem and the resolution: https://stackoverflow.com/questions/55065567/cloud-firestore-node-js-invalid-use-of-type-object-as-a-firestore-argument

For prosperity, I am facing the same problem using Node-Red to send a JSON object like this:

    payload: {
        text: "hello world",
        createdAt: new Date(),
        user: { _id: 1000 } // This causes the issue
    }

Which is then called in the function:

this.firestore.collection(col).add(payload)

And throws the error:

Invalid use of type "object" as a Firestore argument

The workaround, as the SO thread explains is to edit
node_modules/@google-cloud/firestore/build/src/serializer.js

and change isPlainObject to

function isPlainObject(input) {
    return util_1.isObject(input);
}

Is there a more permanent solution that I can use? This is fine on my development environment, but I will need to deploy this to an environment where I cannot edit node_modules

Many thanks

@google-oss-bot
Copy link

I found a few problems with this issue:

  • I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.
  • This issue does not seem to follow the issue template. Make sure you provide all the required information.

@schmidt-sebastian
Copy link
Contributor

@kiwicopple Thanks for sending this over.

Can you run the following on your local machine:

const obj = {
	payload: {
        text: "hello world",
        createdAt: new Date(),
        user: { _id: 1000 } // This causes the issue
    }
};

console.log('Node version: ' + process.version);
console.log('Prototype matches: ' + (Object.getPrototypeOf(obj) === Object.prototype));

For me this prints:

Node version: v10.15.3
Prototype matches: true

@kiwicopple
Copy link
Author

Thanks for the swift response @schmidt-sebastian

After sleeping on this I think it may be better/easier to solve this within the library using firebase-admin (found here: https://github.com/sichangi/node-red-contrib-cloud-firestore/blob/master/src/Write/WriteNode.js).

For more context and anyone who reads this, here was my solution

The issue is with the library above is that they do an Object.assign({}, load) with the payload, but this doesn't "go deep". So this is the result:

// See line 44 of the above where msg.payload = { "foo": "bar", "user": { "_id", 1000 } }
const payload = this.preparePayload(msg.payload)

// Top level has a "prototype"
console.log('Prototype matches: ' + (Object.getPrototypeOf(obj) === Object.prototype));
// -> Prototype matches: true

// Nested objects don't
console.log('Prototype matches: ' + (Object.getPrototypeOf(obj.user) === Object.prototype));
// -> Prototype matches: false 

console.log('Node version: ' + process.version);
//  -> Node version: v8.16.0 - note this was different to my environment a NodeRed was running in a docker container

And I think the only way to solve it is to call JSON.parse(JSON.stringify(msg.payload))

Thanks for the help @schmidt-sebastian - I'll close this for now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants