0

I am using de api of google for books to read info about the books I call a url with ISBN number and this return a JSON I create a hash where I save the data that I want.

The next step if get this hash value to save in database.. any idea how to get this value and get out. this is my code

            var id = "0716604892";
            var post_url = "https://www.googleapis.com/books/v1/volumes?q=isbn:";
            post_url = post_url + id;

            findBook("0716604892");

          function findBook(elem) {
            var id, post_url, request;
            post_url = "https://www.googleapis.com/books/v1/volumes?q=isbn:";
            id = elem;
            post_url = post_url + id;
            request = function (){
                $.ajax({
              url: post_url,
              method: 'GET',
              data: {
                dataType: "json"
              },
              success: function(html, data) {
                var author, hashPetName, image, title;
                title = html['items'][1]['volumeInfo']['title'];
                author = html['items'][1]['volumeInfo']['authors'][0];
                image = html['items'][1]['volumeInfo']['imageLinks']['smallThumbnail'];
                hashPetName = {
                  'title': title,
                  'author': author,
                  'image': image
                };
              },
              error: function(errorThrown) {
                console.log(errorThrown);
              }
            });
            return hasBookName
          };
        };
          elemento_save = request;

I want that elemento_save = hasBookName

Cœur
  • 37,241
  • 25
  • 195
  • 267
Stone
  • 749
  • 1
  • 11
  • 30

2 Answers2

-1

Also, if you want request to hold the result of your anonymous function, you need to make a function call:

function findBook(elem) {
        var id, post_url, request;
        id = void 0;
        post_url = void 0;
        post_url = "https://www.googleapis.com/books/v1/volumes?q=isbn:";
        id = elem;
        var hasBookName = null;
        post_url = post_url + id;
        request = function (){
          $.ajax({
          url: post_url,
          method: 'GET',
          data: {
            dataType: "json"
          },
          success: function(html, data) {
            var author, hashPetName, image, title;
            title = html['items'][1]['volumeInfo']['title'];
            author = html['items'][1]['volumeInfo']['authors'][0];
            image = html['items'][1]['volumeInfo']['imageLinks']['smallThumbnail'];
            hasBookName = {
              'title': title,
              'author': author,
              'image': image
            };

          },
          error: function(errorThrown) {
            console.log(errorThrown);
          }
        })
        return hasBookName
       }();
    element_save = request;
Shubham Khatri
  • 270,417
  • 55
  • 406
  • 400
  • hi @Shubham Khatri I try your solution and I obtain this error, Uncaught ReferenceError: request is not defined, any idea! – Stone Jul 27 '16 at 18:18
  • @Stone rename request to request_element in all the three places and see – Shubham Khatri Jul 27 '16 at 18:21
  • Thanks @Shubham Khatri I do it, but I still have the same error,no matter the name of the variable – Stone Jul 27 '16 at 18:28
  • @Stone which line gives you this error – Shubham Khatri Jul 27 '16 at 18:29
  • The last one `element_save = request;` and in my code I don't have the last brackets that you have before `element_save = request;` @Shubham Khatri – Stone Jul 27 '16 at 18:45
  • @Stone You need to have those brackets. I dont know the reason for it, but they are required. Also request is declared inside findBook function and you are using it outside. Declare it outside the function – Shubham Khatri Jul 27 '16 at 18:46
  • thanks for all @Shubham Khatri I fix this error with variable and brackets but the function don't return anything. – Stone Jul 27 '16 at 20:03
-1

Finally I found the solution, see code in coffescript

salida = findBook("0716604892")
alert(salida['title'])



findBook = (elem) ->
  id = undefined
  post_url = undefined
  request_element = undefined
  post_url = 'https://www.googleapis.com/books/v1/volumes?q=isbn:'
  id = elem
  post_url = post_url + id
  result = ''
  $.ajax
    url: post_url
    async: false
    success: (data) ->
      author = undefined
      hashPetName = undefined
      image = undefined
      title = undefined
      title = data['items'][1]['volumeInfo']['title']
      author = data['items'][1]['volumeInfo']['authors'][0]
      image = data['items'][1]['volumeInfo']['imageLinks']['smallThumbnail']
      hashPetName =
        'title': title
        'author': author
        'image': image
      result = hashPetName
      return
  result
Stone
  • 749
  • 1
  • 11
  • 30