2

I'm trying to do a little web site 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 grateful or at least give me some hints!

Here is my code made up from several ejs, nodejs beginner's 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!')
})
1
  • Have you tried to enable a provider? torrentSearchApi.enableProvider('Torrent9'); or specific provider? Commented Nov 22, 2018 at 23:19

1 Answer 1

4

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!')
})
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much, will try this when i get home and i'll keep you posted if i understand right i must call an async function with await otherwise the promise wont be resolved ?
The promise will be resolved either way, but without „await“ the execution of the program resumes before it is resolved

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.