Create multiple records


#1

Hi everyone,

Bit stuck on this, so I thought I’d see what you guys think.

I have a Polling app where one of the question types allows the user creating the poll to add up to 10 options.

The question record and all the option records need to be created with the same button press. This is easy enough when we know how may options there are, but a lot harder here!

On our question type with two options, the button creates the question, brings back the question ID and then creates the option records with the question ID (for joining later on).

The hard part here is allowing the user to create options before the question has been created!

I thought about making the options a local collection and the when the question has been created add the question ID to each record and then push them all to the Options collection.

Is this something that’s doable, and if so can you guys point me to how I would do this?

If not, any other ideas?

Hope this makes sense!


#2

If I understand what you’re trying to do, you could just use an object literal {} to store the options the user adds as they go then when they hit the button to save it to the DB just use that object for the document you’re inserting.

EDIT: Re-reading your question, you have all your options in a separate collection? What does an options document contain?


#3

Thanks for your reply!

An option contains the following field:

  • text label
  • optional image
  • votes (number)
  • votedBy (array)
  • questionId (to join)

The only elements that would need to be added by the user are the text label and the optional image. The user would set these, and add a number of options from 2 - 10. Then the save button creates them as records.

We’ve structured in this way to avoid arrays, and take advantage of reactivity.