Opening a form with pre-filled values from fields saved in db

Hi everyone,

I am struggling with opening a form with pre-filled values from fields saved in db(using Blaze and Meteor). I am inserting data with separate form. Once entries are made, I have set edit button in form of their displays. What i want to do is, Update that entry in db. For that when user click on edit button the edit form should open with pre-field entries. Now user can edit entries from here and update info.
I am new to meteor and used to do development in PHP. So i was doing same with JS and PHP, but here meteor is not picking those things.
What i am doing for making it work anyhow, is taking a helper in each Field and get its value from db(with the help of ID ) and the return back from helper functions. but i don’t think, this is a ok solution in anyway. Because ,if i write helper for each field, then inside that helper i have to use separate Mongo query to get data. Then return to main page. that means if form has 10 field, helpers are gonna hit db 10 times for data.
So, personally i don’t consider this ok solution.

If someone else know any good practice for this, please share.
Thanks in advance for those who are gonna put any effort.

1 Like

Hi @AkhileshChandel

being a beginner myself pls do not expect my answer to be full completed or best practice :wink:
i think i would create one helper function, which will return the document. Then you can access the attributes of the document via blaze spacebars.
eg.
Document:
FormAnswers: {_id: ‘abc’, formField1: ‘entry1’, formField2: ‘entry2’}

Helper:
Template.returnFormAnswers.helpers({
getAnswers(){
Return FormAnswers.findOne({_id: 'abc});
}
})

Template=returnFormAnswers

  • {getAnswers.formField1}
  • {getAnswers.formField2}
  • Again, 2c from a beginner.

    Hi fox,
    thanks for responding. doesn’t matter if you are beginner or what, what matters here is you are willing to help.

    i am doing similar things . i am confused in one part.

    w{getAnswers.formField1}
    {getAnswers.formField2}

    These two line are helpers or what…?
    Even if you are directly using values. Why one curly braces in each side…?

    once again thanks for responding.

    The helper function is getAnswers - it represents the cursor of the identified document (FormAnswers.findOne({query})) as you want to show a specific attribute of the document I used .FormField1. So coming back to your question it is one** helper function.

    Do you have an example of your collection to show?

    edit:
    just one more thing: in case you are having your answers stored in some sort of array it is very easy to iterate through this array and show the respective values. Here is an example which taught me how to do it:
    http://meteortips.com/first-meteor-tutorial/templates/

    yes, down below is my collection:-1:

    user_medical { _id: any random id,height: 6, weight: 130, }
    assume i want to display user medical details. so there would be many entries. The fields would be height and weight.i want to use _id as primary key and acess rest two on form.

    within the helper of your template:
    getUserMedicals(){
    Return user_medical.find();
    }

    within the template:
    {{#each getUserMedicals}}

  • {{_id}}
  • {{height}}
  • {{weight}}
  • {{/each}

    This should result in something like:

    • 1
    • 6
    • 130

    • 2
    • 7
    • 120

    thanks a lot…

    FYI, calling database methods on the client won’t send any queries to the database and are done with a fast in-memory lookup. This is the beauty of Meteor’s data model.

    When you subscribe to data in your template, a copy of that data is sent to the client via the publication. That is stored in minimongo.
    When you then call db methods on the client, it looks for the data in minimongo and returns it.

    I found this article very helpful in understanding what data is doing in a Meteor app:

    1 Like

    In other words, it’s perfectly fine to do 10 database lookups. I would still prefer an approach more like @foxinthebox103 suggested with the one helper to get data, then using the data in the template