0

I'm working on a small app for a school-related project, and I'm having some trouble understanding what I am doing wrong. I'm using the npm-apac module (using meteor-npm), and it's working properly (returning results to server-side console), but it's not doing things client-side that I want it to do. Related code is as follows:

Meteor Method (Server)

Meteor.startup ->

    Meteor.methods

        "isbnLookup": (isbn) ->

            console.log isbn

            opHelper.execute 'ItemLookup',
                SearchIndex: 'Books',
                ResponseGroup: 'Medium,Images',
                IdType: 'ISBN',
                ItemId: isbn
            , (res) ->
                console.log res
                return res

Meteor Event (Button clicked on form to submit ISBN#)

Template.addbook.events

    'click #isbn-btn': ->

        theISBN = $("#isbn").val().trim()
        console.log theISBN
        Meteor.call 'isbnLookup', theISBN, (err, res) ->
            if (err)
                console.log(err.reason)
            else
                console.log 'SUCCESS!'
                Session.set('isbnResult', res)

Template helper for grabbing result from Meteor.call, which is supposedly stored in Session

Template.addbook.helpers
    amazonSection: ->
        Session.get('isbnResult')

Section of page that calls the above helper, which I thought would display the (unformatted, json junk) results from the above helper:

<div class="col-lg-6">
    {{ amazonSection }}
</div>

I really don't know what I'm doing wrong. I'm sort of new to dealing with this Meteor.call stuff, and since it's using a NPM module and other things, I thought I was handling callbacks the way I was supposed to. When I check the server console, the console.log is indeed outputting data from Amazon, so I know that APAC is working correctly, I just am not able to transfer that result over to the client for display, apparently.

Any help would be greatly appreciated.

MisutoWolf
  • 1,133
  • 1
  • 18
  • 33

1 Answers1

1

opHelper.execute is asynchronous so isbnLookup will always return undefined. The solution is discussed in this related question.

Meteor: Calling an asynchronous function inside a Meteor.method and returning the result

Alternatively, if you stored the results of the lookup in a collection, you would see the result in the collection when the lookup completes.

Template.addbook.events 

  "click #isbn-btn": ->

    theISBN = $("#isbn").val().trim()
    Session.set "isbn", theISBN

    return  if Books.findOne(ItemId: theISBN)

    Meteor.call "isbnLookup", theISBN, (err, res) ->
      if err
        console.log err.reason
      else
        console.log "SUCCESS!"
      return


Meteor.startup ->

  Meteor.methods isbnLookup: (isbn) ->

    opHelper.execute "ItemLookup",
      SearchIndex: "Books"
      ResponseGroup: "Medium,Images"
      IdType: "ISBN"
      ItemId: isbn
    , Meteor.bindEnvironment((res) ->
      Books.insert res
    )



Template.addbook.helpers
  amazonSection: ->
    Books.findOne(ItemId: Session.get("isbn"))
Community
  • 1
  • 1
Neil
  • 2,137
  • 16
  • 24
  • When I attempt to use this method to solve my problem, I get the following error: https://gist.github.com/misutowolf/1f816611e5b7d5268b8d – MisutoWolf Aug 09 '14 at 21:48
  • @MisutoWolf my bad. Wrapped the callback to opHelper.execute with Meteor.bindEnvironment. – Neil Aug 10 '14 at 03:30
  • Making progress for sure, now the AWS response via APAC is giving me trouble. Current output is as follows: (I added a line that dumps the response to console (from using the button) for inspection of AWS responses) https://gist.github.com/misutowolf/935697d7b969b48aedf8 It should be noted the app doesn't crash, but the .insert() doesn't work. Maybe I will have to build my own JSON from parsing the response, then insert that? Looks like there's a bunch of '$' keys that the APAC module sticks in the returned data? – MisutoWolf Aug 10 '14 at 11:01
  • I marked this as a solution, though, because it does solve the problem I asked. This thing with the .insert seems to be an issue with APAC, not the async/Meteor.method stuff I was dealing with. Still trying to fix this, though. – MisutoWolf Aug 10 '14 at 11:08
  • @MisutoWolf I think you're right. Just copy the response fields that you want into a new object (with valid keys!) and insert that. – Neil Aug 10 '14 at 16:36