代码之家  ›  专栏  ›  技术社区  ›  Michal

GraphTool TypeError:将循环结构转换为JSON

  •  0
  • Michal  · 技术社区  · 6 年前

    我从官方文档中复制粘贴的示例,希望看到一些不同的错误(无效的URL或无效的身份验证密钥或类似的错误),但我会遇到一些网页/沙盒错误:

    const fetch = require('isomorphic-fetch')
    const Base64 = require('Base64')
    const FormData =require('form-data')
    
    const apiKey = '__MAILGUN_API_KEY__'
    const url = '__MAILGUN_URL__'
    
    export default event => {
    
      const form = new FormData()
      form.append('from', 'Nilan <nilan@graph.cool>')
      form.append('to', 'Nikolas <nikolas@graph.cool>')
      form.append('subject', 'Test')
      form.append('text', 'Hi')
    
      return fetch(url, {
        headers: {
          'Authorization': `Basic ${Base64.btoa(apiKey)}`
        },
        method: 'POST',
        body: form
      })
    }
    

    enter image description here

    即使是简单的API请求也会失败:

    require('isomorphic-fetch')
    
    module.exports = function (event) {
      const url = 'https://jsonplaceholder.typicode.com/posts'
      return fetch(url)
    }
    

    上述代码还返回:

    TypeError: Converting circular structure to JSON
    
    at Object.stringify (native)
    
    at /data/sandbox/lib/sandbox.js:532:48
    
    at /data/io/8e0059b3-daeb-4989-972f-e0d88e27d15e/webtask.js:46:33
    
    at process._tickDomainCallback (node.js:481:9)
    

    如何从自定义GraphTool订阅/解析程序成功调用API?

    1 回复  |  直到 6 年前
        1
  •  0
  •   Michal    6 年前

    这是最简单的工作示例:

    require('isomorphic-fetch')
    
    module.exports = function (event) {
      const url = 'https://jsonplaceholder.typicode.com/posts'
      return fetch(url)
        .then(res => res.json())
        .then(data => {
          console.log(data)
          return {
            data: {
              sum: 3  
            }
          }
        })
    }