2

My DB structure is as below. hasDevice field is array of reference to users documents.

enter image description here

I trying to remove duplication of same references with hasDevices = [...new Set(hasDevices)]. But it didn't work.

Here is my code.

  user.get()
  .then(snapshot => {
    if (!snapshot.exists) {
      return Error('not existing uid');
    }
    let hasDevices = snapshot.get('hasDevices');
    if (!hasDevices) {
      hasDevices = [];
    }
    hasDevices.push(db.collection(constants.DB_COLLECTION_DEVICES).doc(deviceId));
    hasDevices = [...new Set(hasDevices)];
    console.log('hasDevices', hasDevices];
    return user.update('hasDevices', hasDevices);
  })

How can I remove duplicated references?

EDIT. console.log('hasDevices', hasDevices]; left below logs.

hasDevice [ DocumentReference {
    _firestore:
     Firestore {
       _settings: [Object],
       _settingsFrozen: true,
       _serializer: [Serializer],
       _projectId: 'sharp-imprint-234606',
       _lastSuccessfulRequest: 1557034776367,
       _preferTransactions: false,
       _clientPool: [ClientPool] },
    _path: ResourcePath { segments: [Array] } },
  DocumentReference {
    _firestore:
     Firestore {
       _settings: [Object],
       _settingsFrozen: true,
       _serializer: [Serializer],
       _projectId: 'sharp-imprint-234606',
       _lastSuccessfulRequest: 1557034776367,
       _preferTransactions: false,
       _clientPool: [ClientPool] },
    _path: ResourcePath { segments: [Array] } },
  DocumentReference {
    _firestore:
     Firestore {
       _settings: [Object],
       _settingsFrozen: true,
       _serializer: [Serializer],
       _projectId: 'sharp-imprint-234606',
       _lastSuccessfulRequest: 1557034776367,
       _preferTransactions: false,
       _clientPool: [ClientPool] },
    _path: ResourcePath { segments: [Array] } },

EDIT2.

I finally resolve it as below.

    hasDevices = hasDevices.filter((v,idx) => {
      if (idx === 0) return true;
      for (let i = 0; i < idx; i++) {
        if (v.id === hasDevices[i].id) {
          return false;
        }
      }
      return true;
    });
    return user.update('hasDevices', hasDevices);
sungyong
  • 2,267
  • 8
  • 38
  • 66
  • Did you try logging the value of `hasDevices` before it's written back to the database? Did it contain what you expected? If not, what does your code actually do that's different than what you expect? – Doug Stevenson May 04 '19 at 17:17
  • `hasDevices` has duplicated DocumentReferences before it's written back to the database. What I expect is (1)read array of document reference from DB, (2)push new item to (1) - at this point hasDevices might contain duplicated item if exist. (3)I expect `[...new Set(hasDevices)] remove duplicated items, but it didn't. – sungyong May 05 '19 at 00:30
  • Did you log the value of `hasDevices` just before writing it out? What exactly did the log say? Please be specific. Show a screenshot if necessary. – Doug Stevenson May 05 '19 at 00:44
  • Thanks for your advice. Please show appended logs. – sungyong May 05 '19 at 05:45
  • 1
    Looks like you can't just use JavaScript array and Set to work with DocumentReference type objects in that array. You will have to write some code to deal with those references in whatever way you see fit. https://firebase.google.com/docs/reference/js/firebase.firestore.DocumentReference – Doug Stevenson May 05 '19 at 06:15
  • Thanks. I can resolve by my own filter. – sungyong May 05 '19 at 08:28

1 Answers1

1

The hasDevices in your data structure is array not DocumentReference.

So you should produce a duplicate-free version of the array yourself.

See:

zkohi
  • 2,486
  • 1
  • 10
  • 20