简体   繁体   English

如何查询 Express.js 中的特定内容?

[英]How do I query something specific in Express.js?

I have had the other issue resolved with the Leaderboard API not refreshing properly.我已经解决了排行榜 API 未正确刷新的另一个问题。 I now have another issue with express Querying where I am trying to search a specific Discord ID.我现在在尝试搜索特定的 Discord ID 时遇到了另一个快速查询问题。 Using quick.db for my database and reusing part of the express.js code below that works on my other project to search for Different Ban Evidences by ID.将 quick.db 用于我的数据库,并重用下面适用于我的其他项目的部分 express.js 代码,以按 ID 搜索不同的禁令证据。

https://website.com/api/levelstats?id=DISCORDID

When I put the Discord ID into the above URL.当我将 Discord ID 放入上述 URL 时。 It should parse the URL and output the following result in an Object:它应该解析 URL 并在对象中输出以下结果: 在此处输入图片说明

app.get("/api/levelstats",  async function(request, response) {
  response.setHeader('Content-Type', 'application/json');
  if (!request.query.id) return response.send('null');
  let data;
  if (request.query.id !== 'all') data = await db.fetch('60Levelings', {
    target: request.query.id
  });
  else data = await db.fetch('60Levelings');
  if (!request.query.id !== 'all' && data) data.code = qs.parse(data.code).raw;
   response.send(JSON.stringify(data));
})

I've tried a different method which is params but to no success.我尝试了一种不同的方法,它是 params 但没有成功。 I would appreciate any help, thanks.我很感激任何帮助,谢谢。

You can try this :你可以试试这个:

app.get("/api/levelstats",  async function(request, response) {
  response.setHeader('Content-Type', 'application/json');
  if (!request.query.id) return response.send('null');
  let data;
  // change !== to != , maybe you can compare value only and ignore type data
  if (request.query.id != 'all') {
    // if request.query.id is not all, change type data request.query.id from string to int
    data = await db.fetch('60Levelings', {
      target: parseInt(request.query.id)
    });
    data.code = qs.parse(data.code).raw;
    response.send(JSON.stringify(data));
  }
  else {
    data = await db.fetch('60Levelings');
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM