Data from Webhook not being passed to MongoDB node

Describe the problem/error/question

Data from Webhook not being passed to MongoDB node

What is the error message (if any)?

There is no error message, a webhook receives data, but the next node (MongoDB) receives an empty value for the email, even though you can see the data in the output of the previous node.
Tried the following:

  • “Hardcoding the email directly in the MongoDB query works perfectly.”
  • “Using a Set/Edit Fields node to clean the email and pass it on still results in an empty value at the MongoDB node.”
  • “Using a Code node to explicitly clean and structure the data also failed.”
  • “I’ve confirmed the output of the previous node shows the correct email address.”

Please share your workflow

({
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "991fddae-0981-45e0-8ae6-ee7e4703dccd",
        "responseMode": "lastNode",
        "options": {}
      },
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        0,
        0
      ],
      "id": "c4a56bce-0887-45e1-b992-f2048a77383b",
      "name": "Webhook",
      "webhookId": "991fddae-0981-45e0-8ae6-ee7e4703dccd"
    },
    {
      "parameters": {
        "jsCode": "// The input from the MongoDB node is the found document object itself.\nconst mongoResult = $json;\n\n// We just need to check if the 'history' property on that object is a non-empty array.\nconst hasHistory = mongoResult && Array.isArray(mongoResult.history) && mongoResult.history.length > 0;\n\nif (hasHistory) {\n  // The history exists, so we pass it along.\n  return {\n    json: {\n      success: true,\n      message: \"HISTORY_EXISTS\",\n      email: mongoResult.email, // It's better to use the email from the document we just found.\n      history: mongoResult.history\n    }\n  };\n} else {\n  // This 'else' now correctly handles cases where a user document is found but has an empty history.\n  // It also handles the (unlikely) case that the history field is missing.\n  return {\n    json: {\n      success: true,\n      message: \"NO_HISTORY\",\n      email: $node[\"EmailNode\"].json.email, // Using the original email is fine here.\n      history: []\n    }\n  };\n}"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        672,
        0
      ],
      "id": "ed74cc63-08d1-46a3-8c20-a3b63ef79ef3",
      "name": "Final"
    },
    {
      "parameters": {
        "collection": "chatHistory",
        "options": {},
        "query": "{\n    \"email\": \"{{ $json.email }}\"\n}"
      },
      "type": "n8n-nodes-base.mongoDb",
      "typeVersion": 1.2,
      "position": [
        448,
        0
      ],
      "id": "23864a43-c7a0-42e5-af6f-876360e18148",
      "name": "MongoDB1",
      "alwaysOutputData": true,
      "credentials": {
        "mongoDb": {
          "id": "UlvRWq6GrApPjgue",
          "name": "MongoDB account"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// This is the most reliable way to get all data from the previous node.\nconst items = $input.all();\n\n// We only expect one item from the webhook.\nconst firstItem = items[0];\n\n// Get the email from the webhook's body.\nconst email = firstItem.json.body.email;\n\n// If the email is missing, stop.\nif (!email || typeof email !== 'string') {\n  return []; // Return empty to stop the workflow.\n}\n\n// Clean the email.\nconst cleanedEmail = email.trim().toLowerCase();\n\n// This is the most important part. We are manually constructing\n// the exact data structure that the next node needs.\nconst outputData = {\n  json: {\n    email: cleanedEmail\n  }\n};\n\n// Return the data in the standard n8n array format.\nreturn [outputData];"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        224,
        0
      ],
      "id": "b4339493-2961-4c71-9829-813167f743df",
      "name": "Code"
    }
  ],
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "Code",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Final": {
      "main": [
        []
      ]
    },
    "MongoDB1": {
      "main": [
        [
          {
            "node": "Final",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code": {
      "main": [
        [
          {
            "node": "MongoDB1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "pinData": {},
  "meta": {
    "templateCredsSetupCompleted": true,
    "instanceId": "fd55d99ac08d0decacc32337a58f113f05e5d4b9f64f7ef9ae188b77120c6bfd"
  }
})

Share the output returned by the last node

[ {
“email”: “sandydudy@gmail.com”
}]

Information on your n8n setup

  • n8n version: 1.103.2
  • Database (default: SQLite): SQLite
  • n8n EXECUTIONS_PROCESS setting (default: own, main): Own
  • Running n8n via (Docker, npm, n8n cloud, desktop app): Self-hosted on Hostinger
  • Operating system:

Hey @fusionpaths hope all is good, welcome to the community.
In the mongodb node, change the type of the Query input field from Fixed to Expression.

Thanks mate. It worked like a charm. It was right in front of me and didn’t realise I was using fixed all the time.