Undefined response on Meteor.methods


#1

i’m trying to create a small app to exchange books

I previously used a request directly on google books baut i would like to use an NPM module called “node-isbn” as it’s connected to 3 databases.

In my code, i grab the ISBN from an input and i want to use it in my request.

I checked my code but i don’t understand why i get an undefined error.

I create my call like this :
Meteor.call(‘getISBN’, input , function(error, response) {
if(error){
console.log(‘ERROR :’, error);
}else{
console.log(‘response :’, response);
Session.set(“isbns”, response.items);
var getData = Session.get(“isbns”);
var getTitle = getData[0].volumeInfo.title;
var getDescription = getData[0].volumeInfo.description;
var getAuthors = getData[0].volumeInfo.authors;
var getIdentifier = getData[0].volumeInfo.industryIdentifiers[1].identifier;
var getImage = getData[0].volumeInfo.imageLinks.thumbnail;
console.log(getImage);
books.insert({title: getTitle, authors: getAuthors, isbn13: getIdentifier, description: getDescription, image: getImage},function (error,result){
if (error) console.log(error);
if (result) console.log(result);
})
}
})

}

});

and, my server.js is like that :

Meteor.startup(function () {
// code to run on server at startup

var isbn = Npm.require('node-isbn');

Meteor.methods({
        'getISBN': function(input){ // récupération du paramètre passé par le meteor.call
        //console.log(bookList);
        isbn.resolve(input), function(error, book) {
            if (error){
                console.log('Book not found',error);
            }else{
                  return '%j';
            }
        }
    }

})

})
on my browser, in my console, i get this message :

response : undefined
meteor.js?hash=ec96c6f…:913 Exception in delivering result of invoking ‘getISBN’: TypeError: Cannot read property ‘items’ of undefined
As i want to learn from my mistakes, can you tell me what’s wrong ?

Thanks


#2

This is your problem. Right after you call resolve, you immediately return from the method, which will have an undefined return.

var isbn = Npm.require('node-isbn');
var resolve = Meteor.wrapAsync(isbn.resolve);

Meteor.methods({
  'getISBN': function(input) {
    return resolve(input);
  }
});

This will resolve your problem by making isbn.resolve interact with the fiber as expected within Meteor code. It will also ensure that if it errors, that error will be thrown properly.


#3

Thanks a lot. It’s working now

So i understand that i was trying to call the node-isbn package and asking for a reply that cannot be done this was

i’m not completely confident with sync and async work but i will have a look at fiber

thanks again


#4

Basically any time you have a callback situation you are in async world. However, Meteor methods and publications run in fibers to provide a synchronous development style. This is particularly handy for avoiding callback hell (or the well known callback tree) which is the single biggest argument people have against Node.js in general.

https://meteorhacks.com/fibers-eventloop-and-meteor/ is a really good article about it.