Express React.js MySql data into variable

Hii! I was coding my web app when I ran into some trouble.

**I want to make it so this class will run only if “swishes” ,which is a value from a table in my MySQL db, is above 0.**

     var thepos = 1;
    export default class WishStick extends React.Component {
      render() {      
    var vars = standardwishitems(stars);
    const WishShow = WishResults(stars, vars[0]);
    const anim = pullanim(stars);
    giveItem(vars[1], thepos);
    thepos +=1;
    return(
        <div>
            <Animation />
            {WishShow}
        </div>

    );
}
 }

This is my server code :

const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const app = express();
const mysql = require('mysql');
const {
createPool
} = require('mysql');
const { response } = require('express');

const db = createPool({
host:"My ip address :P",
user:"my username :P",
password:"my password :P",
database:"player"
})

const account = createPool({
host:"My ip address :P",
user:"my username :P",
password:"my password :P",
database:"account"
})

app.use(cors());
app.use(express.json());
app.use(express.urlencoded({
extended: true
}));

app.get('/api/get', (req, res)=>{

const wishes = "SELECT swishes, pwishes FROM account WHERE id = 11";
account.query(wishes, (err, result) => {
    res.send(result);
    
});
});

app.post("/api/insert", (req, res)=>{

const pos = req.body.pos
const vnum = req.body.vnum
const additem = "INSERT INTO player.item (owner_id, pos, count, vnum) VALUES ('11', ?, '1', 
?)"
db.query(additem, [pos, vnum], (err, result) => {
    console.log(pos, vnum, err);
})
res.send("a mers");
});
app.listen(3001, () =>{
console.log("running on port 3001");
});

This is the result from /api/get :

[{"swishes":999,"pwishes":999}]

I have already tried to search online, but every method I saw and tried took the query into array and mapped into h1 or li tags, which is not what I am trying to do. I am trying to get the value from “swishes” into a variable , let’s say wishNum, so then for example I can do if(wishNum > 0) {
return WishStick }else {alert(‘You do not have enough wishes!’)} .
Do you know how I may go about doing this?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

I finally got it guys! This is how you do it:

wishes.map(val => { s_wishes = parseInt(val.swishes)})

Example :

var s_wishes;
const { error, isLoaded, wishes } = this.state;
if (error) {
return <div>Error: {error.message}</div>;
} else if (!isLoaded) {
return <div>Loading...</div>;
} else {
wishes.map(val => { s_wishes = parseInt(val.swishes)});
console.log(s_wishes);
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x