Skip to content
Advertisement

Inserting a request.form.items() list into a mongodb collection / Adding list of lists into mongoDB

I have the following code:

JavaScript

I use for x in request.form.items() to loop through a dynamically generated form. Users create custom fields that can be added. I then add the results to a list of which an example of a print out is:

[('name', 'David'), ('phone', '90'), ('email', 'inf'), ('company', 'Ran'), ('Kids Names', 'Kids Names'), ('Kids Birthday', '2022-08-03'), ("Wife's Email", 'Test@test.com')]

My issue is when I attempt to run the code I get a syntax error:

JavaScript

I am fairly confident the issue arises that my list is improperly formated (or my insert is). I’ve tried converting it using json_dumps, etc and nothing seems to work. Assistance would be appreciated!

Advertisement

Answer

The issue I was facing above was because I was using a list vs a dictionary. I modified my code to be:

JavaScript

And the problem was solved. The key was using nList = {} and nList.update() vs using nList = [] and nList.append()

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement