0

I'm trying to return a resolve or reject depending on if the promise was successful or not. I can't seem to figure out why this isn't returning the response. All I get from my promise is [object Object]. This is what I get in response.

Here's the code:

app.get('/', (req,res) => {
    return new Promise((resolve,reject) => {
        var sql = "INSERT INTO usersinfo (firstname,lastname,email,number,latitude,longitude) VALUES(?,?,?,?,?,?)";
        conn.query(sql,[fname,lname,email,num,req.query.latitude,req.query.longitude], (err,result) => {
            if (err) {
                res.send('error')
                console.log(err,'there has been an error')
                reject('There was an error')
                return
            }else{
                console.log('inserted')
                resolve({ success:'true' })
            }
            res.end()
        })
    })
})

Where I'm fetching the url:

const res = await fetch(`http://MYIPADDRESS?latitude=${latitude}&longitude=${longitude}`)
                console.log(res)

I don't seem to get what's wrong. And if I attach the then((response) => console.log(response)) method I get an error in the expo app saying There was an error sending log messages to your development environment PrettyFormatPluginError:value.hasOwnProperty is not a function. (In 'value.hasOwnProperty('tag')','value.hasOwnProperty is undefined)

LOGIC12
  • 41
  • 2
  • 8
  • How does your second snippet relate to your first? – Evert Feb 07 '22 at 18:59
  • I don't think the router cares what the result of the route handler is, does it? Express doesn't do anything with the return value of the handler. `app.get('/', fn)` Installs a route handler for `/` so that express will call `fn` when a request comes in and it will pass `req, res, next` to `fn` but it doesn't look at the return result, so it doesn't matter if you're returning a promise to express. Who are you intending to consume the result of that promise? – Wyck Feb 07 '22 at 19:03
  • What do you expect `await fetch()` to evaluate to? Did you just forget to call `res.send({ success: true })`? – trincot Feb 07 '22 at 19:03
  • @trincot, good point. Or maybe the intent was `res.send(await new Promise(....))` or `return new Promise(....).then(result => res.send(result))` – Wyck Feb 07 '22 at 19:08
  • @Evert The second snippet is where I'm sending a request to the first. – LOGIC12 Feb 07 '22 at 20:10
  • @trincot I would like to receive the object `{ success:'true }` – LOGIC12 Feb 07 '22 at 20:12
  • @Wyck can that be done? Should I wrap the entire promise into `res.send` ? Would that then return the desired output? – LOGIC12 Feb 07 '22 at 20:13

1 Answers1

1

You don't need to create a promise. The send method can be called asynchronously -- when the response is ready:

app.get('/', (req,res) => {
    var sql = "INSERT INTO usersinfo (firstname,lastname,email,number,latitude,longitude) VALUES(?,?,?,?,?,?)";
    conn.query(sql,[fname,lname,email,num,req.query.latitude,req.query.longitude], (err,result) => {
        if (err) {
            res.send('error');
            console.log(err,'there has been an error');
        } else {
            res.send({ success:'true' }); // <---
            console.log('inserted');
        }
    });
});

NB: also, there is no need to call res.end() as res.send() already implies that.

On the client side you'll have to await the JSON content to be generated:

const response = await fetch(`http://MYIPADDRESS?latitude=${latitude}&longitude=${longitude}`);
const result = await response.json(); // <---
console.log(result);
trincot
  • 317,000
  • 35
  • 244
  • 286