Quantcast
Channel: Active questions tagged youtube-api - Stack Overflow
Viewing all articles
Browse latest Browse all 3718

Why does my node.js app crash when trying to use JSON.parse(data) on incoming data from youtube api?

$
0
0

I am trying to use the youtube api to search for videos using a simple front-end with a search box and search button.

I am using the standard https.get() method to achieve this.The problem I am having, is that once that data arrives inside the response.on() block of code, I would normally use JSON.parse on that data, and store in inside another variable where I would have usable javascript object.

the result is the app crashing with this error in the console:undefined:25 "url": "https://i.ytimg.com/vi/fWRISvAygU/ SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) at IncomingMessage.<anonymous>and the standard [nodemon] app crashed - waiting for file changes before starting...

What I can confirm is the following:

-I have tried getting that data in postman (the EXACT same url pasted into postman), and I do receive a long json format as usual

-I have confirmed that the data I am getting from my front-end is translating into strings that fit nicely into the url-I have console logged the status code inside the https.get() method, and I get a 200ok

-I have console logged the (data) that is inside the response.on("data", function(data){})without parsing it yet, and I get buffer data <Buffer 7d 0a 20 20 22 6b .... bla bla

NOTE: I do have to note something important: I have only done this with one project before (getting weather data from a weather API) and in the code from my other project, which worked perfectly,the only difference I see, is that the buffer data I am getting before JSON.parse() is applied to the incoming data, that buffer data had only one sequence. which means it was a one shot <Buffer 7d 0a 20 20 22 6b bla bla more bytes>the current data I am trying to retrieve in my current project is coming in 5 different chunks of buffer data. I can see 5 different opening and closing tags of that buffer data when I am console logging the data before I try to JSON.parse().

Forgive me if I am not using great terminology here, as this is my 6th month of coding, and I am still early in this journey. I have just started dabbling with APIs with Node.js

const express = require("express");const bodyParser = require("body-parser");const https = require("https");const Joi = require("joi");const apiKey = "MyKeyWouldBeHere"; (hiding it)const app = express();app.use(bodyParser.urlencoded({extended:true}));app.use(express.static("public"));app.get("/", (req, res) => {    res.sendFile(`${__dirname}/index.html`);});app.post("/videos", (req, res) => {    const {error} = validation(req.body);    if (error) return res.status(400).send("wrong way to write");    const userInput = req.body.userInput;    https.get(`https://www.googleapis.com/youtube/v3/search?part=snippet&key=${apiKey}&q=${userInput}`, (response) => {        console.log(response.statusCode);        response.on("data", function(data) {            const weatherData = JSON.parse(data);            console.log(data);        });    });});app.listen("3000", () => {    console.log("Running server on port 3000");});function validation(value) {    const schema = Joi.object({        userInput: Joi.string().min(2).required()    });    return schema.validate(value);}

Viewing all articles
Browse latest Browse all 3718

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>