I have a mongodb setup that will hold some user input values. The forms where the user will input data are all on different html pages, once you fill out one, it will send you to the next page. What I cannot figure out is how to put all this data into one document once every field has been filled. I find this complicated because if you go to the next page for the next form, that previous form will not just hold the data waiting for all the other forms to be filled out. To put this in some context, I am using meteor and the packages aldeed:autoform and aldeed:collection2 to have user input transformed into a json format document. At the moment, I can only have all the user input filled out on the same page using autoform then press the submit button for it all to be put on a document at the same time.
-
You can either save state in server, save fields with localStorage or pass data as url params. Plenty of ways to do itjuvian– juvian2016-05-14 01:34:03 +00:00Commented May 14, 2016 at 1:34
-
@juvian which way do you think is the most efficient?dylan– dylan2016-05-14 01:52:10 +00:00Commented May 14, 2016 at 1:52
2 Answers
On the first form insert to a collection with the Meteor.userId() (assuming your users are logged in). On the next forms you can simply update the collection using methods.
Client:
Meteor.call('firstForm',var1,var2);
Meteor.call('secondForm',var1,var2);
Server:
Meteor.methods({
'firstForm': function (var1,var2) {
collection.insert({
createdBy: Meteor.userId(),
var1: var1,
var2: var2
});
},
'secondForm': function (var3,var4) {
collection.update({
createdBy: Meteor.userId()
}, {
$set: {
var3: var3,
var4: var4
}
});
}
});
Comments
Much better solution will be to maintain a session object using reactive-var. May it be n forms, you can simply update session object. In above code what if user goes back and make changes to first form? It will again insert or may even fail. Above code just shows positive work flow, but you have to consider all possibilities. You can do all n updates to session object, which don't matter. The db call is made to last form where you insert just once and you are done. Before accepting a solution you should throughly examine the usecases fullfilled by an answer.