Skip to content
New issue

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

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

Already on GitHub? # to your account

FieldPath doesn't allow special characters, Firebase Console does #423

Closed
jaschaio opened this issue Dec 14, 2018 · 5 comments
Closed

FieldPath doesn't allow special characters, Firebase Console does #423

jaschaio opened this issue Dec 14, 2018 · 5 comments
Assignees

Comments

@jaschaio
Copy link

jaschaio commented Dec 14, 2018

When trying to update a nested field, I get the following error:

Error: Update() requires either a single JavaScript object or an alternating list of field/value pairs that can be followed by an optional precondition. Argument "tags.`[Bonus] Download`" is not a valid FieldPath. Paths can't be empty and must not contain '*~/[]'.

But within the Firebase Console map keys can actually contain these special characters.

The error is the same, no matter if I use new FieldPath( 'tags', '[Bonus] Download' ) or just build the update object myself like:

var tagName = '[Bonus] Download';
var updatedDoc = {};
updatedDoc[ `tags.${ tagName }`  ] = false;

ref.update( updatedDoc );

I think I read somewhere in the documentation that I should escape paths with special charcters in backticks, but it still throws the same error.

Operating System version: Cloud Functions Node 8.14.0
Firebase SDK version: 6.3.0
Firebase Product: Firestore

@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.

@wilhuff
Copy link

wilhuff commented Dec 17, 2018

The way to do what you want is to pass FieldPaths and values as separate arguments to the update call. If you pass an object, its keys have to be strings and those keys have to be "simple" ones.

Here's how I did it:

async function main() {
  const albums = db.collection('albums');
  const album = albums.doc();

  await album.set({'title': 'Foo'});
  await album.update(new FieldPath('tags', '[Bonus] Download'), true);

  const snap = await album.get();
  console.log(snap.id, snap.data());
};

Running this I got:

fXOPjS5knc6BUuegnyhB { tags: { '[Bonus] Download': true }, title: 'Foo' }

What's going on here is that if you pass an object literal to the update call, all of the keys have to be strings, and all have to have the simple form that doesn't include metacharacters. update also takes field paths and values in pairs, in which case you can pass arbitrary FieldPath objects which handle escaping for you.

Note that you can side-step this issue entirely by using Firestore's (relatively recent) support for array queries and operations. In this case you'd take the tag out of the field path, and make it an array value. The code to the above looks like this:

async function main() {
  const albums = db.collection('albums');
  const album = albums.doc();

  await album.set({'title': 'Foo'});
  await album.update({
    'tags': FieldValue.arrayUnion('[Bonus] Download')
  });

  const snap = await album.get();
  console.log(snap.id, snap.data());
};

The output looks like this:

Gpb2P9GjEi6bfOfr8NlW { tags: [ '[Bonus] Download' ], title: 'Foo' }

@PopeJuliusII
Copy link

@wilhuff Do you know how I would do this with multiple such field paths, i.e. await album.update(updates);? updates won't work as an object, as a key can't be a FieldPath, and a Map won't work either, as update only works with an object.

@wilhuff
Copy link

wilhuff commented Mar 28, 2022

If you want to make multiple updates that aren't known ahead of time, use an array and then spread it into the call:

const updates = [
  new FieldPath('tags', '[Bonus] Download'), true,
  new FieldPath('other', 'path'), "foo",
];
await album.update(...updates);

@PopeJuliusII
Copy link

@wilhuff Thank you for your quick reply! It worked! 💯

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

No branches or pull requests

5 participants