Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
553 views
in Technique[技术] by (71.8m points)

javascript - get data from async function

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

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 !

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!')
})
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...