Blockchain Mad Scientists

A forum for blockchain developers

Whether you've taken a class on Udacity or somewhere else, you are welcome here.
 Create an account now to learn, teach, and mentor!
#11
Hey guys, I think I am getting a connection timeout, but I am not 100% sure...

I am using the following code
Code: Select all
const be = require('blockexplorer');

function getBlock(index) {
  	var bHash = be.blockIndex(index);
  	var block = be.block(JSON.parse(bHash).blackHash);
  	console.log(block);
}

(function theLoop (i) {
	setTimeout(function () {
        getBlock(i);
        i++;
		if (i < 3) theLoop(i);
	}, 3600);
  })(0);


and I am getting the following error

SyntaxError: Unexpected token o in JSON at position 1
at JSON.parse (<anonymous>)
at getBlock (/home/workspace/Project/app.js:9:30)
at Timeout._onTimeout (/home/workspace/Project/app.js:15:9)
at ontimeout (timers.js:475:11)
at tryOnTimeout (timers.js:310:5)
at Timer.listOnTimeout (timers.js:270:5)


When I try to console log the block hash like the following

Code: Select all
function getBlock(index) {
  	var bHash = be.blockIndex(index);
        console.log(bHash);
  	var block = be.block(JSON.parse(bHash).blackHash);
  	console.log(block);
}


I get the following error

Request {
domain: null,
_events:
{ error: [Function: bound ],
complete: [Function: bound ],
pipe: [Function] },
_eventsCount: 3,
_maxListeners: undefined,
uri:
Url {
protocol: 'https:',
slashes: true,
auth: null,
host: 'blockexplorer.com',
port: 443,
hostname: 'blockexplorer.com',
hash: null,
search: null,
query: null,
pathname: '/api/block-index/0',
path: '/api/block-index/0',
href: 'https://blockexplorer.com/api/block-index/0' },
readable: true,
writable: true,
_qs:
Querystring {
request: [Circular],
lib: { formats: [Object], parse: [Function], stringify: [Function] },
useQuerystring: undefined,
parseOptions: {},
stringifyOptions: {} },
_auth:
Auth {
request: [Circular],
hasAuth: false,
sentAuth: false,
bearerToken: null,
user: null,
pass: null },
_oauth: OAuth { request: [Circular], params: null },
_multipart:
Multipart {
request: [Circular],
boundary: 'd07aa571-b9a2-4ccd-9f16-a2e362c48afe',
chunked: false,
body: null },
_redirect:
Redirect {
request: [Circular],
followRedirect: true,
followRedirects: true,
followAllRedirects: false,
followOriginalHttpMethod: false,
allowRedirect: [Function],
maxRedirects: 10,
redirects: [],
redirectsFollowed: 0,
removeRefererHeader: false },
_tunnel:
Tunnel {
request: [Circular],
proxyHeaderWhiteList:
[ 'accept',
'accept-charset',
'accept-encoding',
'accept-language',
'accept-ranges',
'cache-control',
'content-encoding',
'content-language',
'content-location',
'content-md5',
'content-range',
'content-type',
'connection',
'date',
'expect',
'max-forwards',
'pragma',
'referer',
'te',
'user-agent',
'via' ],
proxyHeaderExclusiveList: [] },
_rp_resolve: [Function],
_rp_reject: [Function],
_rp_promise:
Promise {
_bitField: 0,
_fulfillmentHandler0: undefined,
_rejectionHandler0: undefined,
_promise0: undefined,
_receiver0: undefined,
_trace:
{ Error
at Promise.longStackTracesCaptureStackTrace [as _captureStackTrace] (/home/workspace/Project/node_modules/bluebird/js/release/debuggability.js:411:19)
at Promise._resolveFromExecutor (/home/workspace/Project/node_modules/bluebird/js/release/promise.js:480:10)
at new Promise (/home/workspace/Project/node_modules/bluebird/js/release/promise.js:79:10)
at Request.plumbing.init (/home/workspace/Project/node_modules/request-promise-core/lib/plumbing.js:36:28)
at Request.RP$initInterceptor [as init] (/home/workspace/Project/node_modules/request-promise-core/configure/request2.js:41:27)
at new Request (/home/workspace/Project/node_modules/request/request.js:127:8)
at request (/home/workspace/Project/node_modules/request/index.js:53:10)
at request (/home/workspace/Project/node_modules/blockexplorer/blockexplorer.js:289:10)
at Function.API.blockIndex (/home/workspace/Project/node_modules/blockexplorer/blockexplorer.js:20:10)
at getBlock (/home/workspace/Project/app.js:7:19)
at Timeout._onTimeout (/home/workspace/Project/app.js:15:9)
at ontimeout (timers.js:475:11)
at tryOnTimeout (timers.js:310:5)
at Timer.listOnTimeout (timers.js:270:5)
_parent: undefined,
_promisesCreated: 0,
_length: 1,
_promiseCreated: null },
_onCancelField: [Function] },
_rp_callbackOrig: undefined,
callback: [Function],
_rp_options:
{ uri: 'https://blockexplorer.com/api/block-index/0',
callback: [Function: RP$callback],
transform: undefined,
simple: true,
resolveWithFullResponse: false,
transform2xxOnly: false },
headers: { host: 'blockexplorer.com' },
setHeader: [Function],
hasHeader: [Function],
getHeader: [Function],
removeHeader: [Function],
method: 'GET',
localAddress: undefined,
pool: {},
dests: [],
__isRequestRequest: true,
_callback: [Function: RP$callback],
proxy: null,
tunnel: true,
setHost: true,
originalCookieHeader: undefined,
_disableCookies: true,
_jar: undefined,
port: 443,
host: 'blockexplorer.com',
path: '/api/block-index/0',
httpModule:
{ Agent: { [Function: Agent] super_: [Object] },
globalAgent:
Agent {
domain: null,
_events: [Object],
_eventsCount: 1,
_maxListeners: undefined,
defaultPort: 443,
protocol: 'https:',
options: [Object],
requests: {},
sockets: {},
freeSockets: {},
keepAliveMsecs: 1000,
keepAlive: false,
maxSockets: Infinity,
maxFreeSockets: 256,
maxCachedSessions: 100,
_sessionCache: [Object] },
Server: { [Function: Server] super_: [Object] },
createServer: [Function: createServer],
get: [Function: get],
request: [Function: request] },
agentClass:
{ [Function: Agent]
super_: { [Function: Agent] super_: [Object], defaultMaxSockets: Infinity } },
agent:
Agent {
domain: null,
_events: { free: [Function] },
_eventsCount: 1,
_maxListeners: undefined,
defaultPort: 443,
protocol: 'https:',
options: { path: null },
requests: {},
sockets: {},
freeSockets: {},
keepAliveMsecs: 1000,
keepAlive: false,
maxSockets: Infinity,
maxFreeSockets: 256,
maxCachedSessions: 100,
_sessionCache: { map: {}, list: [] } } }
undefined:1
[object Object]
^


Any ideas on how I can get this to work?

Are you looking to create your own cryptocurrency? Join now to access private content!