【问题标题】:How to setup a REST API for Node JS如何为 Node JS 设置 REST API
【发布时间】:2020-08-15 06:50:16
【问题描述】:

我是 REST API 的新手,并且我一直在尝试建立一个并为 discord.js 工作很长一段时间,但我似乎无法弄清楚如何让它工作。任何帮助将不胜感激。

当前代码给出错误FetchError: invalid json response body at https://api.psychonautwiki.org/?=%7B%20%20substances%20%7B%20%20%20name%20%20%20%20effects%20%7B%20%20%20%20%20%20name%20%20%20%20%7D%20%20%7D%7D reason: Unexpected token G in JSON at position 0

这里是主要代码:


const Discord = require('discord.js');
const fetch = require('node-fetch');
const querystring = require('querystring');

    module.exports.execute = async(message, args) => {

        const query = querystring.stringify({ term: args.join(' ') });

        if (!args.length) {
            return message.channel.send('You need to supply a search term!');
        }

        const { list } = await fetch(`https://api.psychonautwiki.org/?={  substances {   name    effects {      name    }  }}`).then(response => response.json());
        if (!list) return message.reply('no');

        message.channel.send(list);
    }

API 设置:

    substances(limit:300) {
        name
        url
        summary
        featured
        addictionPotential
        crossTolerance
        dangerousInteraction {
            name
        }
        class {
            chemical
            psychoactive
        }
        tolerance {
            full
            half
            zero
        }
        effects {
            name
            url
        }
    }
}

【问题讨论】:

    标签: javascript node.js rest api discord.js


    【解决方案1】:

    当使用fecth时,你需要多指定一点

    • 记住这是一个 GraphQL,查询你需要使用POST 而不是GET
    • 您还需要确保传递内容类型
    • 然后您需要在正文中发送query 参数,因为它是fetch,您需要转换为字符串

    所以完整的查询将像这样结束:

    fetch('https://api.psychonautwiki.org', { 
      method: 'POST', 
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ 
        query: "{ substances { name effects { name } }}"
      })
    })
    .then(res => res.json())
    .then(json => console.log(json))
    

    并将返回:

    {
      "data": {
        "substances": [
          {
            "name": "1,4-Butanediol",
            "effects": [
              {
                "name": "Empathy, affection and sociability enhancement"
              }, 
              ...
          },
          ...
        ]
      }
    }
    
    

    您甚至可以在浏览器控制台中运行:


    添加

    这是how to query GraphQL API's 上的一个很好的资源

    【讨论】:

    • 我现在没有收到所有信息。 Promise { <pending> } { data: { substances: [ [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object] ] }, extensions: { tracing: { version: 1, startTime: '2020-04-30T20:32:58.593Z', endTime: '2020-04-30T20:32:58.652Z', duration: 58990406, execution: [Object] } } }
    • 我确定有一个参数需要传递给分页...
    • 如果您使用{ substances(offset:0, limit: 10, query: "1B-LSD") { name effects {name} } },例如,您将获得 78 种效果...只需检查 Schema 并使用它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-21
    • 2019-06-03
    • 2017-08-04
    相关资源
    最近更新 更多