Meteor + Aldeed Autoform :: How to pass parent data?

602 views Asked by At

So on my page, there's a list of different books getting rendered from Books collection.

For each book, there's a form to add some information about the borrower.

My current template structures are as follows:

<template name="bookList">
{{#each book}}
    <div class="book">
    ...
    </div>
    {{> borrowerForm}}
{{/each}}
</template>

<template name="borrowerForm">
    <div class="borrowerForm">
    {{#autoForm schema=borrowerFormSchema id="borrowerForm" type="method" meteormethod="addBorrower" class="hide"}}
    </div>
{{/each}}
</template> 

I am using type="method" for this "borrower" form so that I can modify the doc (aka the form input value) with additional object.

Here's how I am doing it currently:

Meteor.methods({
    addBorrower: function(doc) {
        if (! Meteor.userId()) {
            throw new Meteor.Error("not-authorized");
        }
        Meteor.users.deny({
            insert: function() {
                return true;
            }
        });
        check(doc, Schemas.borrowerSchema);

        var myData = doc;

        //var bookID = Template.parentData(1)._id;
        //console.log("bookID: " + bookID);

        //myData["bookID"] = bookID;  
        myData["createdBy"] = Meteor.userId();
        myData["createdAt"] = new Date();

        Borrower.insert(myData);
    }
});

All seems to work just fine until I need to add one more thing which is the book ID. I'd like to include the book ID that is being borrowed as an additional data alongside createdBy and createdAt.

The way I grab the book ID is by using Template.parentData(1)._id and it does return the correct data (See the commented lines on the above code). However, it's throwing this error below which I don't understand and the form just won't submit at all:

M…r.m…e.errorClass {
      error: 500, 
      reason: "Internal server error", 
      details: undefined, 
      message: "Internal server error [500]", 
      errorType: "Meteor.Error"
}
 details: undefined
 error: 500
 errorType: "Meteor.Error"
 message: "Internal server error [500]"
 reason: "Internal server error"stack: (...)
 ▶︎ get stack: () { [native code] }
 ▶︎ set stack: () { [native code] }
 ▶︎ __proto__: Meteor.makeErrorType.errorClass

Terminal:

  Exception while invoking method 'addBorrower' ReferenceError: Template is not defined
      at [object Object].Meteor.methods.addBorrower (app/common/books.js:42:31)
      at maybeAuditArgumentChecks (packages/ddp/livedata_server.js:1617:1)
      at packages/ddp/livedata_server.js:648:1
      at [object Object]._.extend.withValue (packages/meteor/dynamics_nodejs.js:56:1)
      at packages/ddp/livedata_server.js:647:1
      at [object Object]._.extend.withValue (packages/meteor/dynamics_nodejs.js:56:1)
      at [object Object]._.extend.protocol_handlers.method (packages/ddp/livedata_server.js:646:1)
      at packages/ddp/livedata_server.js:546:1

Could someone kindly explain what's going on there and how to solve the issue, please? Thanks!

I am a novice in Javascript and Meteor so a simple but clear explanation (with analogies) would be much appreciated too.

1

There are 1 answers

6
kaoskeya On BEST ANSWER

Firstly move your method into a file in server/ folder. Also, move your deny rule outside the method.

Now, in your borrowerForm.onRendered create a AutoForm hook like:

AutoForm.hooks({
    borrowerForm: {
        before: {
            method: function( doc ) {
                doc.bookID = Template.parentData(1)._id;
                return doc;
            }
        }
    }
});

This should work, I haven't tried it though. If it still doesn't, please put your code on MeteorPad and post a link.