I am trying to get a Lambda function to access an API and return the JSON.
API
http://api.openweathermap.org/data/2.5/weather?id=2172797&appid=b1b15e88fa797225412429c1c50c122a
P.S This API ID is the demo one provided by OW.
Lambda Code
var jsonurl = "http://api.openweathermap.org/data/2.5/weather?id=2172797&appid=b1b15e88fa797225412429c1c50c122a";
var data = JSON.parse(jsonurl);
exports.handler = function(event, context) {
console.log(data);
context.done(null, data); // SUCCESS with message
};
Error
{
"errorMessage": "Unexpected token h",
"errorType": "SyntaxError",
"stackTrace": [
"Object.parse (native)",
"Object.<anonymous> (/var/task/index.js:3:17)",
"Module._compile (module.js:456:26)",
"Object.Module._extensions..js (module.js:474:10)",
"Module.load (module.js:356:32)",
"Function.Module._load (module.js:312:12)",
"Module.require (module.js:364:17)",
"require (module.js:380:17)"
]
}
Log output
START RequestId: 8ca0fbd1-eee5-11e5-b9dd-31048a8d5a45 Version: $LATEST
Syntax error in module 'index': SyntaxError
at Object.parse (native)
at Object.<anonymous> (/var/task/index.js:3:17)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
END RequestId: 8ca0fbd1-eee5-11e5-b9dd-31048a8d5a45
REPORT RequestId: 8ca0fbd1-eee5-11e5-b9dd-31048a8d5a45 Duration: 173.76 ms Billed Duration: 200 ms Memory Size: 128 MB Max Memory Used: 9 MB
Can anyone see the problem?
What I would like is for lambda to get the json and return it, so anyone looking at my API url will see the results from the Open Weather API
You are passing a URL to JSON.parse(), not a JSON string. First you need to go get the JSON data from the URL using something like http.get(). Perhaps check out the answers to this similar question: Parsing JSON data from a URL
Related
During the install of this github project I get this error:
Error: Invalid JSON RPC response: undefined
at Object.InvalidResponse (C:\Users\backu\blockchain\Blockchain4LinkedData-master\Dapp\node_modules\web3\lib\web3\errors.js:35:16)
at HttpProvider.send (C:\Users\backu\blockchain\Blockchain4LinkedData-master\Dapp\node_modules\web3\lib\web3\httpprovider.js:91:22)
at RequestManager.send (C:\Users\backu\blockchain\Blockchain4LinkedData-master\Dapp\node_modules\web3\lib\web3\requestmanager.js:58:32)
at Eth.send [as getBlock] (C:\Users\backu\blockchain\Blockchain4LinkedData-master\Dapp\node_modules\web3\lib\web3\method.js:145:58)
at Object. (C:\Users\backu\blockchain\Blockchain4LinkedData-master\Dapp\app\routes.js:31:22)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Module.require (internal/modules/cjs/loader.js:952:19)
Could someone help me with this?
Most likely, when the geth was launched, one of the modules whose methods were used by Dapp was not enabled in the settings of the RPC API. Look at the value of the --rpcapi key from the startup line.
I'm trying to read CSV with Javascript, but I'm getting an error.
Here is my code:
fs.readFile('./data/AMZN.csv', async (err,data)=>{
if(err){
console.log(err)
return
}
console.log(data.replace("$",""));
})
Here is the example of my CSV data:
Date, Close/Last, Volume, Open, High, Low
09/11/2020, $3116.22, 5093982, $3208.69, $3217.3409, $3083.98
09/10/2020, $3175.11, 5330741, $3307.219, $3349.89, $3170.55
09/09/2020, $3268.61, 5188675, $3202.99, $3303.175, $3185
And here is the error that I get:
SyntaxError: Unexpected number
at wrapSafe (internal/modules/cjs/loader.js:1072:16)
at Module._compile (internal/modules/cjs/loader.js:1122:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
at Module.load (internal/modules/cjs/loader.js:1002:32)
at Function.Module._load (internal/modules/cjs/loader.js:901:14)
at Module.require (internal/modules/cjs/loader.js:1044:19)
at require (internal/modules/cjs/helpers.js:77:18)
at Object.<anonymous> (C:\Users\jackl\Desktop\Projects\scraper_cheerio\scraper.js:5:14)
at Module._compile (internal/modules/cjs/loader.js:1158:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
I've figured out that Javascript perceives $3116 as a string and the following .22 as a property of that string object, which can not be a number and therefore throws "Unexpected number".
But how do I fix it? I've been trying to deal with it for 2 days, but no luck so far.
The problem was in this line:
const AMZN = require("./data/AMZN.csv");
The code in the question works fine.
Nodejs version : 4.4.7
NPM version: 2.15.8
I am running the following code, which is literally copied from the following URL
https://kafka.js.org/docs/getting-started
const { Kafka } = require('kafkajs')
const kafka = new Kafka({
clientId: 'my-app',
brokers: ['kafka1:9092', 'kafka2:9092']
})
The above code is saved in "apps.js" file under a "simpleapp" folder. In addition to that I have installed the following
npm install kafkajs
When I run the app.js file I get the following error
/Users/nick/Documents/nodeprojects/simpleapp/apps.js:1
(function (exports, require, module, __filename, __dirname) { const { Kafka } = require('kafkajs')
^
SyntaxError: Unexpected token {
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:373:25)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:139:18)
at node.js:968:3
What is the issue?
Version 4.4.7 of NodeJS is very old, and isn't even supported anymore. KafkaJS follows the same support schedule as NodeJS itself, so the oldest currently supported version is 8.
My main objective is to parse a JSON file and write the content of the JSON file to a scrollable list in the browser. I created a JSON file and stored it in an S3 bucket. I then created the following Lambda function:
// ----receive function----v
function get_json(url, callback) {
http.get(url, function(res) {
var body = '';
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
var response = JSON.parse(body);
// call function ----v
callback(response);
});
});
}
var mydata = get_json("https://s3.amazonaws.com/scrollablelist/scrollableList.json", function (resp) {
console.log(resp);
});
I get the following error:
{
"errorMessage": "http is not defined",
"errorType": "ReferenceError",
"stackTrace": [
"Object.<anonymous> (/var/task/index.js:20:18)",
"Module._compile (module.js:570:32)",
"Object.Module._extensions..js (module.js:579:10)",
"Module.load (module.js:487:32)",
"tryModuleLoad (module.js:446:12)",
"Function.Module._load (module.js:438:3)",
"Module.require (module.js:497:17)",
"require (internal/module.js:20:19)"
]
}
The Log Output says this:
START RequestId: 516f164b-7ee3-11e7-97a7-0373caaf493b Version: $LATEST
module initialization error: ReferenceError
at get_json (/var/task/index.js:3:9)
at Object.<anonymous> (/var/task/index.js:20:18)
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)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
END RequestId: 516f164b-7ee3-11e7-97a7-0373caaf493b
REPORT RequestId: 516f164b-7ee3-11e7-97a7-0373caaf493b Duration: 80.81 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 19 MB
Any help would be greatly appreciated.
You have to install npm package for http in your project folder. Please follow below steps,
a. Open node command prompt
b. Navigate (CD ) to your project folder
c. Type command "npm init" if you don't have a package.json file already
e. Type command "npm install http --save" (Which will add http module to package.json)
f. At the beggining of you js file you using "http" please refer http moduleas below,
var http = require("http");
g. Add package.json file as well in the .zip to upload to Lambda (You don't need to include node modules folder)
You have to install the http module. npm install --save http
https://aws.amazon.com/blogs/compute/nodejs-packages-in-lambda/
I'm newbie of javascript and I want to open json file in node.js.
What's the problem of this "SyntaxError: Unexpected token"?
$ cat jsfile
{'key1':'val1', 'key2':'val2'}
obj = require("./jsfile");
jsfile:1
(function (exports, require, module, __filename, __dirname) {
{'key1':'val1', 'key2':'val2'}
^
SyntaxError: Unexpected token :
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:373:25)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at repl:1:7
at REPLServer.defaultEval (repl.js:262:27)
at bound (domain.js:287:14)
Your file isn't valid JSON.
JSON strings should be delimited by double quotes, not single quotes.
So your file should look like this:
{"key1":"val1", "key2":"val2"}
Also, JSON files should have a .json extension:
$ cat jsfile.json
{"key1":"val1", "key2":"val2"}
obj = require('./jsfile.json');