Return an api value

Asked

Viewed 73 times

0

Hello, I wrote the following code:


var request = require('request')
var cheerio = require('cheerio')
const express = require("express")
const app = express()
const PORT = process.env.PORT || 8000

let ranks = [];
request('https://shadowarena.pearlabyss.com/en-US/Arena?battleType=0&server=sa',function( err, res, body){
    if(err) console.log('Erro: ' + err);
    var $ = cheerio.load(body);
    $('.box_list_area').each(function(){
        var name = $(this).find('.thum_name').text()
        ranks.push(name)

    });
    for(var i = 0; i <ranks.length; i++){
        if(ranks[i] === "YoDaSL"){
            let rankYo = i+1
            app.listen(PORT,()=>{
                console.log("Escutando")
            })
            app.get('/', (req, res) => {
                res.json({
                    msg: "Top: ", rankYo
                })

            })

            break
        }
    }


});




On the part of app.get() the value returned from the api is being:{top","rankYo":7} how do I return a clean value, example Top: 7?

  • Don’t get it ? a clean value? a text value? because your code is for json!

  • Yes, so I did it in json, but the answer I don’t like, because when I get the api, it returns me {top","rankYo":7}, as I could do to return Top: 7. I used json because it was what I remembered, but it doesn’t have to be.

  • thus res.send("Top: 7");

1 answer

0

The problem is that you are passing an object inside the Sponse

res.json( {msg: "Top: ", rankYo} )

Simply change your current answer to res.json(`Top: ${rankYo}`)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.