New to JSON in nodejs getting some errors - javascript

getJSON('https://api.twitch.tv/kraken/streams/Jonathan_x64',
function(channel) {
if (channel["stream"] == null) {
//do something
} else {
////do something else
}
});
that is my current code but when i run it i get the following error
if (channel["stream"] == null) {
^
TypeError: Cannot read property 'stream' of undefined
at E:\my ultemet bot\index.js:10:16
at Request._callback (E:\my ultemet bot\node_modules\get-JSON\lib\node.js:11:5)
at Request.self.callback (E:\my ultemet bot\node_modules\request\request.js:200:22)
at emitTwo (events.js:106:13)
at Request.emit (events.js:191:7)
at Request.<anonymous> (E:\my ultemet bot\node_modules\request\request.js:1067:10)
at emitOne (events.js:101:20)
at Request.emit (events.js:188:7)
at IncomingMessage.<anonymous> (E:\my ultemet bot\node_modules\request\request.js:988:12)
at emitNone (events.js:91:20)

As far as I know there isn't a built-in top level getJSON() function in Node so you must be using a custom function.
From the stack trace you've shared:
at Request._callback (E:\my ultemet bot\node_modules\get-JSON\lib\node.js:11:5)
^^^^^^^^^^^^^^^^^^^^^
... we learn that you are using an NPM third-party module. Once there, it's trivial to find the documentation:
var getJSON = require('get-json')
getJSON('http://api.listenparadise.org', function(error, response){
error
// undefined
response.result
// ["Beth Orton — Stolen Car",
// "Jack White — Temporary Ground",
// "I Am Kloot — Loch",
// "Portishead — Glory Box"]
response.ok
// => true
})
It isn't real code but it's clear that the first callback argument is error, but you have this:
function(channel){}
Since (as the error message states) it's undefined, that means that the call is successful—it's just you aren't reading it correctly.
I've been peeking the module source code and it's actually not very impressive. It's basically a tiny wrapper for request that doesn't add much value.

Related

How to create an Azure AppendBlob from node.js

I have installed the npm azure-storage package.
On Azure I have created a Storage Account and a container.
I then try to create an Append Blob:
const azure = require('azure-storage');
const service = azure.createBlobService("[ACCOUNT]", "[KEY]");
service.createAppendBlobFromText("[CONTAINER]",
"some-blob-name",
"some-text",
{},
(err, result) => {
console.log('err ->',err);
console.log('result ->',result);
});
The result of calling this is:
err -> { Error
at Function.StorageServiceClient._normalizeError (/[REMOVED]/node_modules/azure-storage/lib/common/services/storageserviceclient.js:1191:23)
at BlobService.StorageServiceClient._processResponse (/[REMOVED]/node_modules/azure-storage/lib/common/services/storageserviceclient.js:738:50)
at Request.processResponseCallback [as _callback] (/[REMOVED]/node_modules/azure-storage/lib/common/services/storageserviceclient.js:311:37)
at Request.self.callback (/[REMOVED]/node_modules/request/request.js:186:22)
at emitTwo (events.js:125:13)
at Request.emit (events.js:213:7)
at Request.<anonymous> (/[REMOVED]/node_modules/request/request.js:1163:10)
at emitOne (events.js:115:13)
at Request.emit (events.js:210:7)
at IncomingMessage.<anonymous> (/[REMOVED]/node_modules/request/request.js:1085:12)
at Object.onceWrapper (events.js:314:30)
at emitNone (events.js:110:20)
at IncomingMessage.emit (events.js:207:7)
at endReadableNT (_stream_readable.js:1045:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
name: 'StorageError',
message: 'Append blobs are not supported.\nRequestId:ed1777f4-601c-00cf-19a0-bb77ba000000\nTime:2018-03-14T14:25:50.8138962Z',
code: 'BlobTypeNotSupported',
statusCode: 400,
requestId: 'ed1777f4-601c-00cf-19a0-bb77ba000000' }
result -> null
I have not been able to find anything, when searching for the error.
Am I missing something here?
Please check the redundancy kind of the storage account in which you're trying to create this blob.
Blob type support varies by the storage account redundancy kind.
For example, ZRS Classic redundancy kind of storage account only supports Block Blob while Premium LRS redundancy kind of storage account only supports Page Blob.

Uncaught NetworkingError: Cannot read property 'replace' of undefined Error

I'm getting the following error when trying to run the following code. My goal is to delete all items in both the User and Item tables. User and Item correspond to Dynamoose models.
The for loop is running correctly. But the scan().exec callback function isn't being run at all and it's throwing the following error.
What is really strange is on CircleCI this problem doesn't happen. It only happens on my local computer. I have tried removing node_modules and running npm install with no luck. Also it works fine if I remove the code below.
Any ideas? Or even where to start debugging this?
Code:
var dbarray = [User, Item];
for (var i = 0; i < dbarray.length; i++) {
dbarray[i].scan().exec(function(err, items) {
if (err) {
throw err;
}
items.forEach(function(item, key) {
item.delete();
});
});
}
Error:
General
1) "before each" hook
0 passing (10s)
1 failing
1) "before each" hook:
Uncaught NetworkingError: Cannot read property 'replace' of undefined
at findTargetPort (node_modules/zombie/lib/reroute.js:50:28)
at Socket.Net.Socket.connect (node_modules/zombie/lib/reroute.js:69:18)
at Agent.connect [as createConnection] (net.js:106:35)
at Agent.createSocket (_http_agent.js:217:26)
at Agent.addRequest (_http_agent.js:187:10)
at new ClientRequest (_http_client.js:272:16)
at Object.request (http.js:39:10)
at features.constructor.handleRequest (node_modules/aws-sdk/lib/http/node.js:42:23)
at executeSend (node_modules/aws-sdk/lib/event_listeners.js:304:29)
at Request.SEND (node_modules/aws-sdk/lib/event_listeners.js:318:9)
at Request.callListeners (node_modules/aws-sdk/lib/sequential_executor.js:101:18)
at Request.emit (node_modules/aws-sdk/lib/sequential_executor.js:77:10)
at Request.emit (node_modules/aws-sdk/lib/request.js:683:14)
at Request.transition (node_modules/aws-sdk/lib/request.js:22:10)
at AcceptorStateMachine.runTo (node_modules/aws-sdk/lib/state_machine.js:14:12)
at node_modules/aws-sdk/lib/state_machine.js:26:10
at Request.<anonymous> (node_modules/aws-sdk/lib/request.js:38:9)
at Request.<anonymous> (node_modules/aws-sdk/lib/request.js:685:12)
at Request.callListeners (node_modules/aws-sdk/lib/sequential_executor.js:115:18)
at callNextListener (node_modules/aws-sdk/lib/sequential_executor.js:95:12)
at node_modules/aws-sdk/lib/event_listeners.js:220:9
at finish (node_modules/aws-sdk/lib/config.js:315:7)
at node_modules/aws-sdk/lib/config.js:333:9
at Credentials.get (node_modules/aws-sdk/lib/credentials.js:126:7)
at getAsyncCredentials (node_modules/aws-sdk/lib/config.js:327:24)
at Config.getCredentials (node_modules/aws-sdk/lib/config.js:347:9)
at Request.SIGN (node_modules/aws-sdk/lib/event_listeners.js:192:22)
at Request.callListeners (node_modules/aws-sdk/lib/sequential_executor.js:101:18)
at Request.emit (node_modules/aws-sdk/lib/sequential_executor.js:77:10)
at Request.emit (node_modules/aws-sdk/lib/request.js:683:14)
at Request.transition (node_modules/aws-sdk/lib/request.js:22:10)
at AcceptorStateMachine.runTo (node_modules/aws-sdk/lib/state_machine.js:14:12)
at node_modules/aws-sdk/lib/state_machine.js:26:10
at Request.<anonymous> (node_modules/aws-sdk/lib/request.js:38:9)
at Request.<anonymous> (node_modules/aws-sdk/lib/request.js:685:12)
at Request.callListeners (node_modules/aws-sdk/lib/sequential_executor.js:115:18)
at Timeout.callNextListener [as _onTimeout] (node_modules/aws-sdk/lib/sequential_executor.js:95:12)
The issue turned out to be with Zombie.js. Specially Browser.localhost('localhost', port);. Removing that line and passing in http://localhost:3000 before each visit command solved the problem.
Not sure why the error was mentioning AWS and things like that.

Get a file without name in url

I would like to get the file from this url : https://donnees.roulez-eco.fr/opendata/instantane
As you can see there's no file name in the URL.
My problem is with this simple code :
var file = fs.createWriteStream('./myFile.zip')
http.get(url, function(response) {
response.pipe(file);
}
This code works well if you got an url like https://someurl.com/filename.zip.
The file is downloaded and can be use but this response.pipe(file) line throw me this error:
events.js:163
throw er; // Unhandled 'error' event
^
Error: Parse Error
at TLSSocket.socketOnData (_http_client.js:411:20)
at emitOne (events.js:96:13)
at TLSSocket.emit (events.js:191:7)
at readableAddChunk (_stream_readable.js:178:18)
at TLSSocket.Readable.push (_stream_readable.js:136:10)
at TLSWrap.onread (net.js:559:20)
What should / can i do to fix this?

Running test of hyperledger fabric-sdk-node

I'm trying to run test of the hyperledger fabric client node but I have some issues. Indeed, when I run the command gulp test I have lots of errors which look like this:
not ok 586 Error: SERVICE_UNAVAILABLE at ClientDuplexStream.<anonymous> (C:\Users\A671975\go\src\github.com\hyperledger\fabric-sdk-node\node_
modules\fabric-client\lib\Orderer.js:9:4530) at emitOne (events.js:96:13) at ClientDuplexStream.emit (events.js:188:7) at ClientDuplexStream.
_emitStatusIfDone (C:\Users\A671975\go\src\github.com\hyperledger\fabric-sdk-node\node_modules\grpc\src\node\src\client.js:204:12) at ClientD
uplexStream._readsDone (C:\Users\A671975\go\src\github.com\hyperledger\fabric-sdk-node\node_modules\grpc\src\node\src\client.js:169:8) at rea
dCallback (C:\Users\A671975\go\src\github.com\hyperledger\fabric-sdk-node\node_modules\grpc\src\node\src\client.js:229:12)
---
operator: fail
at: Client.newDefaultKeyValueStore.then.then.then.then.then.then (C:\Users\A671975\go\src\github.com\hyperledger\fabric-sdk-node\test\int
egration\e2e\create-channel.js:211:5)
stack: |-
Error: Error: SERVICE_UNAVAILABLE
at ClientDuplexStream.<anonymous> (C:\Users\A671975\go\src\github.com\hyperledger\fabric-sdk-node\node_modules\fabric-client\lib\Or
derer.js:9:4530)
at emitOne (events.js:96:13)
at ClientDuplexStream.emit (events.js:188:7)
at ClientDuplexStream._emitStatusIfDone (C:\Users\A671975\go\src\github.com\hyperledger\fabric-sdk-node\node_modules\grpc\src\node\
src\client.js:204:12)
at ClientDuplexStream._readsDone (C:\Users\A671975\go\src\github.com\hyperledger\fabric-sdk-node\node_modules\grpc\src\node\src\cli
ent.js:169:8)
at readCallback (C:\Users\A671975\go\src\github.com\hyperledger\fabric-sdk-node\node_modules\grpc\src\node\src\client.js:229:12)
at Test.assert [as _assert] (C:\Users\A671975\go\src\github.com\hyperledger\fabric-sdk-node\node_modules\tape\lib\test.js:212:54)
at Test.bound [as _assert] (C:\Users\A671975\go\src\github.com\hyperledger\fabric-sdk-node\node_modules\tape\lib\test.js:64:32)
at Test.fail (C:\Users\A671975\go\src\github.com\hyperledger\fabric-sdk-node\node_modules\tape\lib\test.js:277:10)
at Test.bound [as fail] (C:\Users\A671975\go\src\github.com\hyperledger\fabric-sdk-node\node_modules\tape\lib\test.js:64:32)
at Client.newDefaultKeyValueStore.then.then.then.then.then.then (C:\Users\A671975\go\src\github.com\hyperledger\fabric-sdk-node\tes
t\integration\e2e\create-channel.js:211:5)
at process._tickDomainCallback (internal/process/next_tick.js:129:7)
I think this is due to an error of connexion between the tests and the containers but I don't understand what I have to do in order to solve it. So if someone had this error before, please share the solution ;) Thanks !

Instagram API: JSON.parse() cannot read property of undefined Node Js

I am using the Instagram API to search for a specific tag and then display the first result's image. Here I am using the JSON.parse() function parse the JSON that comes back from making a http get request to https://api.instagram.com/v1/tags/{tag}/media/recent?access_token={access token}. Here is what the request is returning me (- everything that doesn't matter). Note: I blanked out parts of the URL's, assume that they are correct.
{
"data":[
{
"images":{
"low_resolution":{
"url":"https://scontent.cdninstagram.com/t51.2885-15/s320x320/e35/--------_--------6248_1327489376_n.jpg?ig_cache_key=--------",
"width":320,
"height":320
},
"thumbnail":{
"url":"https://scontent.cdninstagram.com/t51.2885-15/s150x150/e35/--------_--------6248_1327489376_n.jpg?ig_cache_key=--------",
"width":150,
"height":150
},
"standard_resolution":{
"url":"https://scontent.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/--------_--------6248_1327489376_n.jpg?ig_cache_key=--------",
"width":640,
"height":640
}
}
}
]
}
Here is the code I am using to make the request and parse the JSON:
request.get("https://api.instagram.com/v1/tags/" + tag + "/media/recent?access_token=" + accessToken, function(error, response, body)
{
console.log(JSON.parse(body).data.images.standard_resolution.url)
});
});
When ever I run this code it gives me this error:
/Users/pjtnt11/Documents/NodeJs/Instagram/index.js:52
console.log(JSON.parse(body).data.images.standard_resolution.url)
^
TypeError: Cannot read property 'images' of undefined
at Request._callback (/Users/pjtnt11/Documents/NodeJs/Instagram/index.js:52:67)
at Request.self.callback (/Users/pjtnt11/Documents/NodeJs/Instagram/node_modules/request/request.js:187:22)
at emitTwo (events.js:106:13)
at Request.emit (events.js:191:7)
at Request.<anonymous> (/Users/pjtnt11/Documents/NodeJs/Instagram/node_modules/request/request.js:1044:10)
at emitOne (events.js:96:13)
at Request.emit (events.js:188:7)
at IncomingMessage.<anonymous> (/Users/pjtnt11/Documents/NodeJs/Instagram/node_modules/request/request.js:965:12)
at emitNone (events.js:91:20)
at IncomingMessage.emit (events.js:185:7)
I thought that maybe it could have been because console.log() was running before JSON.parse() but JSON.parse blocks the code from running anything else. I also check if it had a callback or a promise that I could use but I didn't see anything.
Now I have no idea what is going on. Does anyone know what could be causing this error and how I could fix it?
EDIT:
Okay, so I found out that data is a data array, so I need to change my code to be data[0] the problem now is I get the following error when I run the code:
/Users/pjtnt11/Documents/NodeJs/Instagram/index.js:52
console.log(JSON.parse(body).data[0].images.standard_resolution.url)
^
TypeError: Cannot read property '0' of undefined
at Request._callback (/Users/pjtnt11/Documents/NodeJs/Instagram/index.js:52:67)
at Request.self.callback (/Users/pjtnt11/Documents/NodeJs/Instagram/node_modules/request/request.js:187:22)
at emitTwo (events.js:106:13)
at Request.emit (events.js:191:7)
at Request.<anonymous> (/Users/pjtnt11/Documents/NodeJs/Instagram/node_modules/request/request.js:1044:10)
at emitOne (events.js:96:13)
at Request.emit (events.js:188:7)
at IncomingMessage.<anonymous> (/Users/pjtnt11/Documents/NodeJs/Instagram/node_modules/request/request.js:965:12)
at emitNone (events.js:91:20)
at IncomingMessage.emit (events.js:185:7)
data is an Array, so you need to access the first element...
JSON.parse(body).data[0].images
see following working snippet...
var someObject = '{ \
"data":[ \
{ \
"images":{ \
"low_resolution":{ \
"url":"https://scontent.cdninstagram.com/t51.2885-15/s320x320/e35/--------_--------6248_1327489376_n.jpg?ig_cache_key=--------", \
"width":320, \
"height":320 \
}, \
"thumbnail":{ \
"url":"https://scontent.cdninstagram.com/t51.2885-15/s150x150/e35/--------_--------6248_1327489376_n.jpg?ig_cache_key=--------", \
"width":150, \
"height":150 \
}, \
"standard_resolution":{ \
"url":"https://scontent.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/--------_--------6248_1327489376_n.jpg?ig_cache_key=--------", \
"width":640, \
"height":640 \
} \
} \
} \
] \
}';
console.log(JSON.parse(someObject).data[0].images.low_resolution.url);

Categories