lewisdonovan / google-news-scraper

Lightweight scraper for Google News
Other
243 stars 62 forks source link

TypeError: UnhandledPromiseRejectionWarning on requesting of news inside Express App #18

Closed thatfreakcoder closed 2 years ago

thatfreakcoder commented 2 years ago

When running your code example. i am receiving this error message in the console:

(node:13508) UnhandledPromiseRejectionWarning: TypeError: (intermediate value).toISOString(...).split(...)[0].replaceAll is not a function
    at module.exports (E:\News API\node_modules\google-news-scraper\index.js:36:65)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async E:\News API\index.js:10:22
(Use `node --trace-warnings ...` to show where the warning was created)
(node:13508) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:13508) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

I can work on a solution for it an can submit a PR if you want

lewisdonovan commented 2 years ago

Hi, thanks for flagging. I've tried to reproduce this locally with express, but I'm getting the expected results. I've shared some example code below.

The line that's flagged in your error messages is using the Date() function, which is native to JS, so I'm not sure why this would fail. Could you share some more info about your setup?

package.json

{
  "name": "gns-sandbox",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "nodemon index.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.2",
    "google-news-scraper": "^1.0.8"
  },
  "devDependencies": {
    "nodemon": "^2.0.15"
  }
}

index.js

const gns = require('google-news-scraper');
const express = require('express')
const app = express()

app.get('/', async (req, res) => {
  try {
    const articles = await gns({
      searchTerm: "dogecoin",
      queryVars: {
        hl:"en-US",
        gl:"US",
        ceid:"US:en"
      },
      prettyURLs: false,
      timeframe: "1h",
      puppeteerArgs: [
        '--no-sandbox',
        '--disable-setuid-sandbox'
      ]
    })
    res.status(300).json(articles)
  } catch (err) {
    res.status(400).json(err)
  }
})

app.listen(3000)

Command line: nodemon index.js

mashegoindustries commented 2 years ago

Hi,

I am getting similar error.

I am running this as a Firebase Cloud Function (Typescript)

MY CLOUD FUNCTION CODE

exports.loadGoogleNewsScraper = functions.runWith({ memory: '1GB', timeoutSeconds: 300, }).pubsub.schedule('every 5 minutes').onRun(async (context) => {

console.log('loadGoogleNewsScraper runs every 5 minutes!');

const googleNewsScraper = require('google-news-scraper');

let articles = await googleNewsScraper({ searchTerm: "kaizer chiefs", prettyURLs: false, timeframe: "5d", puppeteerArgs: [ '--no-sandbox', '--disable-setuid-sandbox' ] }) console.log(articles)

return null; });

ERROR FROM FIREBASE CLOUD FUNCTION LOGS

TypeError: (intermediate value).toISOString(...).split(...)[0].replaceAll is not a function at module.exports (/workspace/node_modules/google-news-scraper/index.js:36:61) at processTicksAndRejections (internal/process/task_queues.js:95:5) at async /workspace/lib/index.js:521:20

mashegoindustries commented 2 years ago

I am getting this hint from VS Code

"Property 'replaceAll' does not exist on type 'string'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2021' or later."

I suppose a lot of people might experience the same issue. This is a problem for Typescript users

I could change my config to "ES2020", but then this breaks my entire project... I am currently on es2017

Can we perhaps change the line below to use anything else either than replaceAll

value: YES+cb.${new Date().toISOString().split('T')[0].replaceAll('-','')}-04-p0.en-GB+FX+667,

thatfreakcoder commented 2 years ago

I noticed that some compilers use Node 14 out of the box. Node 14 does NOT have support for the replaceAll function, it got supported after the Node 15 runtime.

The solution that worked for me is two of them.

Hope that helps @mashegoindustries

mashegoindustries commented 2 years ago

thatfreakcoder

Thank you. The first option you suggest looks like the option I will go for.

Don't you want to create a pull request. For this. Hopefully the author of the library will accept it. Like you "Some compilers use Node 14 out of the box"

thatfreakcoder commented 2 years ago

@mashegoindustries I will submit a pull request for this, I opened this issue for the same :)

lewisdonovan commented 2 years ago

Merged in d711fb42a1a840308d12413b11ac713dff1124d4 thanks for contributing!