问题描述
限时送ChatGPT账号..我正在尝试为我的种子箱做一个像sickgearr这样的小网站:我想要一个搜索表单,它将使用这个 api 向我的 torrent 提供商发送搜索查询:https://github/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/JimmyLaurent/torrent-search-api
我设法从表单中获取文本,进行 api 调用并在控制台中打印结果.
i managed getting text from the form, making the api calls and get results printed in the console.
但是当我尝试将它们传递到即将成为结果的页面时,我只是传递了 Promise,我不太了解 Promise 的原理.
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/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屋!
更多推荐
[db:关键词]
发布评论