brianleroux / tiny-json-http

:anchor: Minimalist HTTP client for JSON payloads.
172 stars 18 forks source link

Error: options.url required #13

Closed alexpilugin closed 6 years ago

alexpilugin commented 6 years ago

Error: Error: options.url required at Error (native) at Object.module.exports [as get] (/Developer/tiny-promiss/node_modules/tiny-json-http/bundle.js:1:1000) at Promise (/Developer/Nodejs/2018.02.05 node-quering/010-tiny-promiss/index.js:31:30) at getData (/Developer/Nodejs/2018.02.05 node-quering/010-tiny-promiss/index.js:29:12) at Object. (/Developer/Nodejs/2018.02.05 node-quering/010-tiny-promiss/index.js:15:1) at Module._compile (module.js:570:32) at Object.Module._extensions..js (module.js:579:10) at Module.load (module.js:487:32) at tryModuleLoad (module.js:446:12) at Function.Module._load (module.js:438:3)

'use strict';

var tiny = require('tiny-json-http')

const url =
    "https://maps.googleapis.com/maps/api/geocode/json?address=Florence";

getData(url)
    .then(data =>
        console.log(
            `City: ${data.results[0].formatted_address} -`,
            `Latitude: ${data.results[0].geometry.location.lat} -`,
            `Longitude: ${data.results[0].geometry.location.lng}`
        )
    )
    .catch(error => {
        console.log('Error: ', error);
    });

function getData(url) {
    // return new pending promise
    return new Promise((resolve, reject) => {

        const request = tiny.get(url, (err, response) => {
            if (err) {
                console.log('Error', err);
                reject(err);
            } else {
                const body = response.body;
                console.log(body);
                resolve(body);
            }
        });
    })
}
neurotech commented 6 years ago

Try wrapping the url portion of const request = tiny.get(url, (err, response) => { in curly braces {}:

'use strict';

var tiny = require('tiny-json-http')

const url =
    "https://maps.googleapis.com/maps/api/geocode/json?address=Florence";

getData(url)
    .then(data =>
        console.log(
            `City: ${data.results[0].formatted_address} -`,
            `Latitude: ${data.results[0].geometry.location.lat} -`,
            `Longitude: ${data.results[0].geometry.location.lng}`
        )
    )
    .catch(error => {
        console.log('Error: ', error);
    });

function getData(url) {
    // return new pending promise
    return new Promise((resolve, reject) => {

        const request = tiny.get({url}, (err, response) => {
            if (err) {
                console.log('Error', err);
                reject(err);
            } else {
                const body = response.body;
                console.log(body);
                resolve(body);
            }
        });
    })
}
alexpilugin commented 6 years ago

Oh! Thank you. It works!

neurotech commented 6 years ago

My pleasure - basically the module is expecting the url to be part of an object, but you were passing it through as a string.

brianleroux commented 6 years ago

Hey @alexpilugin ! Just FYI that tiny-json-http "natively" supports Promise. If you don't supply a callback it'll return one for you.

Ex.

var tiny = require('tiny-json-http')
var url = 'https://maps.googleapis.com/maps/api/geocode/json?address=Florence'

function handler(data) {
  var first = data.body.results[0]
  console.log(
    `City: ${first.formatted_address} -`, 
    `Latitude: ${first.geometry.location.lat} -`,
    `Longitude: ${first.geometry.location.lng}`
  )
}

tiny.get({url}).then(handler).catch(console.log)
alexpilugin commented 6 years ago

Hi @brianleroux

Your version looks amazing but from first time I received:

TypeError: Cannot read property 'formatted_address' of undefined
    at handler (/011-tiny-callback/index.js:18:23)
    at process._tickCallback (internal/process/next_tick.js:109:7)

from second launch: $ node index.js it produced an expected result!

brianleroux commented 6 years ago

bizarre! was unable to repro that myself

screen shot 2018-02-09 at 2 16 00 pm
alexpilugin commented 6 years ago

@brianleroux Yes, it was only once. Sorry, I couldn't replicate this error as well.

brianleroux commented 6 years ago

np at all! lmk if you run into anything else or have any other questions. happy to help. 🙏