0

我从官方文档中复制粘贴示例,希望我会看到一些不同的错误(无效的 URL 或无效的身份验证密钥或类似的东西)但是我得到一些 webpack/sandbox 错误:

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
  })
}

在此处输入图像描述

即使是简单的 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)

如何从自定义 graphcool 订阅/解析器成功调用 API?

4

1 回答 1

0

这是最简单的工作示例:

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  
        }
      }
    })
}
于 2018-07-03T14:35:57.793 回答