从异步函数获取数据 [英] get data from async function

查看:32
本文介绍了从异步函数获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的种子箱创建一个像镰刀一样的小网站:我想要一个搜索表单,它将使用此api将搜索查询发送给我的洪流提供商: https://github.com/JimmyLaurent/torrent-search-api

i'm trying to do a little website like sickgearr for my seedbox : i want a search form which will send a search query to my torrent providers using this api : https://github.com/JimmyLaurent/torrent-search-api

我设法从表单获取文本,进行api调用并在控制台中打印结果.

i managed getting text from the form, making the api calls and get results printed in the console.

但是当我尝试将它们传递到即将成为结果的页面时,我只是传递了诺言,而我不太了解诺言的原理.

but when i try to pass them to the soon to-become result page, i'm only passing promises and i don't quite understand the principle of promises.

如果有人可以帮助我解决我的问题,我将不胜感激,或者至少会给我一些提示!

If someone could help me resolve my issues i'd be really really gratefull or atleast give me some hints !

这是我的代码,由几个ejs,nodejs入门教程组成:

Here is my code made up from several ejs, nodejs begginers tutorials :

const express = require('express');
const bodyParser = require('body-parser');
const app = express()
const TorrentSearchApi = require('torrent-search-api');
const tableify = require('tableify');
TorrentSearchApi.enableProvider('Yggtorrent','Login', 'Password');

app.use(express.static('public'));
app.use(bodyParser.urlencoded({ extended: true }));
app.set('view engine', 'ejs')

async function search(query){ // Search for torrents using the api

var string = query.toLowerCase();
//console.log(string);
const torrents = await TorrentSearchApi.search(string,'All',20); // Search for legal linux distros 
return(JSON.stringify(torrents));
}

app.get('/', function (req, res) {
  res.render('index');
})

app.post('/', function (req, res) {
var rawTorrent = search(req.body.torrent);
var page = tableify(rawTorrent); //printing rawtorrent will only give me "promise"
res.render('results',page);
})


app.listen(3000, function () {
  console.log('Example app listening on port 3000!')
})

推荐答案

您的搜索功能正在使用 async / await .这意味着搜索功能是异步的,并返回一个 Promise .您应该等待其结果(第23行).

Your search function is using async/await. It means the search function is asynchrone and returns a Promise. You should await its result (line 23).

https://javascript.info/async-await

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function

const express = require('express')
const bodyParser = require('body-parser')
const app = express()
const TorrentSearchApi = require('torrent-search-api')
const tableify = require('tableify')

TorrentSearchApi.enableProvider('Yggtorrent','Login', 'Password')

app.use(express.static('public'))
app.use(bodyParser.urlencoded({ extended: true }))
app.set('view engine', 'ejs')


const search = async query => {
  const loweredQuery = query.toLowerCase()
  const torrents = await TorrentSearchApi.search(loweredQuery, 'All', 20)
  return JSON.stringify(torrents)
}

app.get('/', (_, res) => res.render('index'))

app.post('/', async (req, res) => {
  const torrents = await search(req.body.torrent) // Right here
  const htmlTable = tableify(torrents)
  res.render('results', htmlTable)
})

app.listen(3000, function () {
  console.log('Example app listening on port 3000!')
})

这篇关于从异步函数获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆