N
N
nicksetc2021-02-10 18:03:07
API
nicksetc, 2021-02-10 18:03:07

Why does Cloudflare Worker throw an error when trying to parse JSON?

There is a code:

addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request))
})

/**
 * Respond to the request
 * @param {Request} request
 */


async function handleRequest(request) {
  const url = new URL(request.url)
  try{
    let quotes = JSON.parse(
      await(await fetch('https://raw.githubusercontent.com/bonsoirelliot/navalny-rest/quotes/quotes1.json')).text()
    )
    const randomQuote = quotes[Math.floor(Math.random() * quotes.length)]
    return new Response(randomQuote, {status: 200})
  }catch(error){
    print(error)
  }
}

When requested, returns code 500(ReferenceError: print is not defined). This only happens if there are lines with Russian characters in the json file. Everything works fine with English.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rsa97, 2021-02-10
@nicksetc

You have a JSON curve, there is a comma before the closing bracket.
Well, the print function in JS is Window.print() by default, which prints the current page in the browser.

A
Alexander Ulo, 2017-09-07
@Salexer

try calling the snippet uncached.
In general, pdoTools has fastfield tags.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question