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.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
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.addbook.helpers
amazonSection: ->
Session.get('isbnResult')
<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.
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"))