0

I would like to pass the lat lon from this URL http://localhost:8080/fp?lon=103.742463567216646&lat=1.336711421273283. Where should I place the "req.query.lon" and "req.query.lat" in the following code?

I am hoping to parse the users input in the URL link so that I can dynamically retrieve from the database.

const {Client} = require("pg")
const express = require ("express")
const url=require('url')
const fs= require('')
const app = express();
app.use(express.json())

const client = new Client({
    "user": "xxx",
    "password" : "xxx",
    "host" : "xxx",
    "port" : xxx,
    "database" : "xxx"
})

//app.get("/", (req, res) => res.sendFile(`${__dirname}/index.html`))

app.get("/fp", async (req, res) => {
    //const lon = 103.742463567216646;
    //const lat = 1.336711421273283;
    //const lon = req.query.lon;
    //const lat = req.query.lat;
    const rows = await readTodos ();

    res.send(JSON.stringify(rows))
})





app.listen(8080, () => console.log("Web server is listening.. on port 8080"))

start()

async function start() {
    await connect();


}


async function connect() {
    try {
        await client.connect();
    }
    catch(e) {
        console.error(`Failed to connect ${e}`)
    }
}

async function readTodos() {
    try {
    const results = await client.query("SELECT ST_AsText(ST_Force2D(geom)) FROM fp ORDER BY geom <-> ST_SetSRID(ST_MakePoint("+lon+ ","+lat+ "), 3993) LIMIT 1;");
    return results.rows;
    }
    catch(e){
        return [];
    }
}
2
  • 1
    The place you have commented is the correct code. You can have them there, pls uncomment them and it works Commented May 4, 2020 at 2:46
  • I don't understand what you're asking for help with. If you are properly passing the URL as shown in your question, then the req.query.lon and req.query.lat are the right way to access query parameters on that URL. Commented May 4, 2020 at 2:58

1 Answer 1

0

Express parses it by default and store them in req.query. Accessing req.query.<key> will give you the value you are looking for. For instance, in your example, express will store it inside req.query.lon and req.query.lat. So, actually you access them right in the request handler of /fp, just comment out the access to these variables and pass them as parameters to readTodos():

app.get("/fp", async (req, res) => {
    const lon = req.query.lon;
    const lat = req.query.lat;
    const rows = await readTodos(lon, lat);

    res.send(JSON.stringify(rows))
})

async function readTodos(lon, lat) {
    try {
    const results = await client.query("SELECT ST_AsText(ST_Force2D(geom)) FROM fp ORDER BY geom <-> ST_SetSRID(ST_MakePoint("+lon+ ","+lat+ "), 3993) LIMIT 1;");
    return results.rows;
    }
    catch(e){
        return [];
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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.