Hello I’m trying to create a user in my mongo DB.
Every time I try mongoDb returns :
[
{
"userHash": null,
"email": null,
"_id": "6012a4e9bba21a2590ba4a7b",
"id": "6012a4e9bba21a2590ba4a7b"
}
]
If I use the code in this answer the code works well : Mongodb query
But If i’m trying to adapt the code to my use case it doesn’t works properly.
Here is what I created :
{
"nodes": [
{
"parameters": {},
"name": "Start",
"type": "n8n-nodes-base.start",
"typeVersion": 1,
"position": [
130,
-380
]
},
{
"parameters": {
"functionCode": "const keyNeeded = ['userHash', 'email'];\nvar gotAllNeededKey = false;\n\nconst result = [];\n\nif (items[0].json.body) {\n if (Object.keys(items[0].json.body).length == keyNeeded.length){\n \n gotAllNeededKey = true;\n const body = items[0].json.body;\n \n for (let i=0; i < keyNeeded.length ; i++){\n if(!body[keyNeeded[i]]) {gotAllNeededKey = false;} \n }\n }\n}\n\nresult.push({ json: { requestIsValid: gotAllNeededKey, body: items[0].json.body} });\n\nreturn result;\n"
},
"name": "[UR] verifyKeyInRequest",
"type": "n8n-nodes-base.function",
"typeVersion": 1,
"position": [
410,
-110
]
},
{
"parameters": {
"conditions": {
"boolean": [
{
"value1": "={{$json[\"requestIsValid\"]}}",
"value2": true
}
]
}
},
"name": "[UR] If keyVerified",
"type": "n8n-nodes-base.if",
"typeVersion": 1,
"position": [
630,
-100
]
},
{
"parameters": {
"collection": "users",
"query": "={\"email\":\"{{$json[\"body\"][\"email\"]}}\"}"
},
"name": "[UR] searchUser",
"type": "n8n-nodes-base.mongoDb",
"typeVersion": 1,
"position": [
820,
-110
],
"alwaysOutputData": true,
"credentials": {
"mongoDb": "MongoDatabase "
}
},
{
"parameters": {
"conditions": {
"boolean": [
{
"value1": "={{$json[\"result\"]}}",
"value2": true
}
],
"number": []
}
},
"name": "[UR] If userNotFound",
"type": "n8n-nodes-base.if",
"typeVersion": 1,
"position": [
1200,
-120
]
},
{
"parameters": {
"operation": "insert",
"collection": "users",
"fields": "={{Object.keys($node[\"[UR] If keyVerified\"].json[\"body\"]).join(', ')}}"
},
"name": "[UR] createUser",
"type": "n8n-nodes-base.mongoDb",
"typeVersion": 1,
"position": [
1430,
-130
],
"alwaysOutputData": false,
"credentials": {
"mongoDb": "MongoDatabase "
}
},
{
"parameters": {
"functionCode": "const results = []\n\nif (Object.keys(items[0].json).length === 0) {\n results.push({ json: { result: true } });\n} else {\nresults.push({ json: { result: false } });\n}\n \nreturn results\n"
},
"name": "[UR] userNotFound",
"type": "n8n-nodes-base.function",
"typeVersion": 1,
"position": [
1010,
-110
]
},
{
"parameters": {
"httpMethod": "POST",
"path": "registeruser",
"responseMode": "=onReceived",
"options": {}
},
"name": "[P] userRegistration [UR]",
"type": "n8n-nodes-base.webhook",
"typeVersion": 1,
"position": [
150,
-100
],
"webhookId": "5a615c4f-035f-4f95-a5ea-c9ed5e7eda30",
"disabled": true
},
{
"parameters": {
"functionCode": "items[0].json = { \n \"body\": {\n \"userHash\": \"4dqretertertd74qs8f7dqfsf87qf\",\n \"email\": \"[email protected]\"\n } \n};\nreturn items;"
},
"name": "FakeWebhook",
"type": "n8n-nodes-base.function",
"typeVersion": 1,
"position": [
300,
-310
]
}
],
"connections": {
"Start": {
"main": [
[
{
"node": "FakeWebhook",
"type": "main",
"index": 0
}
]
]
},
"[UR] verifyKeyInRequest": {
"main": [
[
{
"node": "[UR] If keyVerified",
"type": "main",
"index": 0
}
]
]
},
"[UR] If keyVerified": {
"main": [
[
{
"node": "[UR] searchUser",
"type": "main",
"index": 0
}
]
]
},
"[UR] searchUser": {
"main": [
[
{
"node": "[UR] userNotFound",
"type": "main",
"index": 0
}
]
]
},
"[UR] If userNotFound": {
"main": [
[
{
"node": "[UR] createUser",
"type": "main",
"index": 0
}
]
]
},
"[UR] userNotFound": {
"main": [
[
{
"node": "[UR] If userNotFound",
"type": "main",
"index": 0
}
]
]
},
"[P] userRegistration [UR]": {
"main": [
[
{
"node": "[UR] verifyKeyInRequest",
"type": "main",
"index": 0
}
]
]
},
"FakeWebhook": {
"main": [
[
{
"node": "[UR] verifyKeyInRequest",
"type": "main",
"index": 0
}
]
]
}
}
}
Any idea why it doesn’t works ?