Firestore, Array edit

Hello :orange_heart:,
I have a problem that I don’t know how to solve, I have a db in Firestore, that have users, each user have an array with multiple ids’s of quotes, I just want to add the ID of quote that comes from a webhook to the first place of the array in firestore.

firestore
Screenshot (5)

I see the node of “set” but these only have type string or number, any ideas? Did I have to bring all the id’s of quotes first?

If I understood correctly, you might want t use a merge node. Check the example below.

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        290,
        570
      ]
    },
    {
      "parameters": {
        "mode": "keepKeyMatches",
        "propertyName1": "quote",
        "propertyName2": "quote"
      },
      "name": "Merge",
      "type": "n8n-nodes-base.merge",
      "typeVersion": 1,
      "position": [
        1190,
        570
      ]
    },
    {
      "parameters": {
        "functionCode": "return [\n  {\n     json: {\n       user: 1,\n       quotes: ['1','2']\n     } \n  },\n    {\n     json: {\n       user: 2,\n       quotes: ['3','4']\n     } \n  }\n]"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        600,
        560
      ],
      "notesInFlow": true,
      "notes": "User Data"
    },
    {
      "parameters": {
        "functionCode": "const results = []\n\nfor (const item of items) {\n  const json = item.json;\n  for (const quote of json.quotes) {\n    results.push({ json: { quote, user: json.user } })\n  }\n}\n\nreturn results;"
      },
      "name": "Function1",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        860,
        560
      ],
      "notesInFlow": true,
      "notes": "Map data"
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "quote",
              "value": "1"
            }
          ]
        },
        "options": {}
      },
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        610,
        800
      ],
      "notesInFlow": true,
      "notes": "Webhook mockup"
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          },
          {
            "node": "Set",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function": {
      "main": [
        [
          {
            "node": "Function1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function1": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 1
          }
        ]
      ]
    }
  }
}

:exploding_head:thanks @RicardoE105 now I’m so closer, that was the way for do these, the only problem now is I need to combine both in one array:


How should the array look?

Do not get it :sweat_smile:. Is it not that the output you need?

Is very closer the result after merge is good, but I need to edit function 1 of:

  const results = []
    
for (const item of items) {
      const json = item.json;
      for (const stringValue of json) {
        results.push({ json: { stringValue, user: json.user } })
      }
    }

return results;

Because I get :

"stringValue":{
"stringValue": "id"
}

I just need:
{"stringValue": "id"}

Try it like below:

const results = []
    
for (const item of items) {
      const json = item.json;
      for (const stringValue of json) {
        results.push({ json: { stringValue: stringValue.stringValue } })
      }
    }

return results;

thanks @RicardoE105 for the help. !!!

1 Like

Glad that it worked. Have fun.