I have a file called index.js it contain the query to select data from my table.
In another file I have written code to bring data from my table. When I execute the code I can print some words but I can't print or bring data from the table.
class MyApp extends React.Component{
constructor(props){
super(props)
this.state= {
employees:[],
success : 0
}
this.getList = this.getList.bind(this)
}
getList(){
let config = {
url:'/api/gettable '
};
return new Promise((resolve,reject) =>{
customFetch(config)
.then((res) => {
resolve(res.rows);
}).catch((err) => {
reject(new Error(err));
});
})
}
async componentDidMount(){
try{
let employeeData = await this.getList();
this.setState({
employees:employeeData
})
}
catch(err){
console.log(err);
}
}
render(){
return(
<div className="dashboard animated slideInUpTiny animation-duration-3 ">
<div className="row">
<div className="app-wrapper pb-0">
<div>
<h1>Welcom</h1>
{this.state.employees.map(employee => <div>Id={employee.Id} Name={employee.Name} Description={employee.Description} Date={employee.Date} </div>)}
</div>
</div>
</div>
</div>
);
}
}
export default MyApp ;
It prints welcome but didn't print the data of table.
Try this :
Change this line
this.state.employees.map(employee => <div>Id={employee.Id} Name={employee.Name} Description={employee.Description} Date={employee.Date} </div>)}
To
{this.state.employees && this.state.employees.map(employee => <div>Id={employee.Id} Name={employee.Name} Description={employee.Description} Date={employee.Date} </div>)}
Related
im trying to show images from the database and loop through them with a map. Here is the code:
class Container extends React.Component
{
state ={
userData:[]
}
fethData= async()=>{
fetch("http://localhost:5000/user") // could be any rest get url
.then(response => response.json())
.then(result =>
this.setState({
userData: result
})
);
}
componentDidMount() {
this.fethData();
alert(this.state.userData);
$(function(){
//Make every clone image unique.
var counts = [0];
var resizeOpts = {
handles: "all" ,autoHide:true
};
$(".dragImg").draggable({
helper: "clone",
//Create counter
start: function() { counts[0]++; }
});
$("#dropHere").droppable({
drop: function(e, ui){
if(ui.draggable.hasClass("dragImg")) {
$(this).append($(ui.helper).clone());
//Pointing to the dragImg class in dropHere and add new class.
$("#dropHere .dragImg").addClass("item-"+counts[0]);
$("#dropHere .img").addClass("imgSize-"+counts[0]);
//Remove the current class (ui-draggable and dragImg)
$("#dropHere .item-"+counts[0]).removeClass("dragImg ui-draggable ui-draggable-dragging");
$(".item-"+counts[0]).dblclick(function() {
$(this).remove();
});
make_draggable($(".item-"+counts[0]));
$(".imgSize-"+counts[0]).resizable(resizeOpts);
}
}
});
var zIndex = 0;
function make_draggable(elements)
{
elements.draggable({
containment:'parent',
start:function(e,ui){ ui.helper.css('z-index',++zIndex); },
stop:function(e,ui){
}
});
}
});
}
changeColor(params) {
this.setState({
color: params.target.value
})
}
changeSize(params) {
this.setState({
size: params.target.value
})
}
render() {
return (
<div className="container">
<div className="tools-section">
<div className="color-picker-container">
Select Brush Color :
<input type="color" value={this.state.color} onChange={this.changeColor.bind(this)}/>
</div>
<div className="brushsize-container">
Select Brush Size :
<select value={this.state.size} onChange={this.changeSize.bind(this)}>
<option> 5 </option>
<option> 10 </option>
<option> 15 </option>
<option> 20 </option>
<option> 25 </option>
<option> 30 </option>
</select>
</div>
</div>
<div className="board-container">
<h4>Select picture!</h4>
{this.state.userData.map((data) => (
<div class="dragImg">
<img src={data.picture} class="img"/> // column data received
</div>
))}
<div id="dropHere">
<Board color={this.state.color} size={this.state.size}></Board></div>
</div>
</div>
);
}
}
export default Container
I would like to put data from function fethData into userData array. But when i run the website i get an alert that userData is undefined. Why is nothing added to userData?
This is the json data fetched from the database:
[{"idpictures":1,"picture":"images/kitten.jpg","title_picture":"Cat"},{"idpictures":2,"picture":"images/puppy.jpg","title_picture":"Dog"}]
I would like the data to be stored like this:
userData:[{idpictures:1,picture:"images/kitten.jpg",title_picture:"Cat"}]
Guys I solved it. This is what I changed about my code:
constructor(){
super();
this.state ={
userData:[]
}
}
async componentDidMount() {
const url = "http://localhost:5000/user";
const response = await fetch(url);
const data = await response.json();
this.setState({userData: data});
console.log(this.state.userData);
if (this.state.userData) {alert(this.state.userData)}
You should define the state of the component in the constructor. Also all data fetching in JS is asynchronous. Notice the .then in the fetch function you wrote. It contains code that will be executed once the response comes back.
class Container extends React.Component
{
constructor(props) {
super(props)
this.state ={
userData:[]
}
}
fethData = async() => {
fetch("http://localhost:5000/user") // could be any rest get url
.then(response => response.json()) // you might not need this, depends on the response
.then(result =>
this.setState({
userData: result
})
);
}
componentDidUpdate() {
alert(this.state) // This should show your data (when it gets here)
}
componentDidMount() {
this.fethData();
alert(this.state.userData); // this will fire before the response from localhost:5000/user gets here
}
}
export default Container
Here is the working example
fethData= ()=>{
return new Promise((resolve, reject)=>{
fetch("http://localhost:5000/user") // could be any rest get url
.then(response => response.json())
.then(result =>
this.setState({userData: result},()=>{
resolve();
})
);
})
}
async componentDidMount(){
await this.fetchData();
alert(this.state.userData);
}
ReactJS is different from Vue, use: this.state.userData to access userData. And fetchData is a asynchronous function, you can't get it's result synchronously.
the above img shows the proplem. you can check the console in bottom right too
I am working on one to one chat website using socket io react node,express i am facing this issue where example :when i type first hi then hi displays 1 time when i type hii it gets displayed 2 times when 3rd time i type jo it displays jo 3 times how can i fix this this is my react code Also my messaging is not receiving at other end it is displaying only on senders page
import React, { Component } from 'react';
import { Link,Redirect } from 'react-router-dom';
import UserService from "../services/userservice";
import {getUsersFriend} from "../services/messageservice";
import io from "socket.io-client";
const SOCKET_IO_URL = "http://localhost:4000/";
export default class Messages extends Component {
constructor(props){
super(props)
this.socket = io(SOCKET_IO_URL)
this.state = {
currentUser: UserService.getCurrentUser(),
isLoading:false,
userdetails:[],
show:false,
username:'',
message:'',
socketConnected:false,
messages:[]
};
this.onTextboxChangeMessage = this.onTextboxChangeMessage.bind(this)
}
componentDidMount(){
const {currentUser}=this.state
this.fetchUser()
this.socket.on('connect',()=> {
this.setState({ socketConnected : true})
// console.log("connection")
})
}
async fetchUser(){
try{
const {currentUser} = this.state
console.log(currentUser)
const data = { userid : currentUser.user._id }
console.log(data)
let user = await getUsersFriend(data)
this.setState({ userdetails: user });
// console.log(user)
}catch(err){
console.log(err)
}
}
showMessageSpace(elementusername){
this.setState({
show: true,
username:elementusername
});
}
onTextboxChangeMessage(e){
this.setState({ message:e.target.value})
}
SendMessage(e,senderid,receiverusername,message,senderusername){
e.preventDefault();
e.stopPropagation()
console.log('event', e)
const {messages} =this.state
if(this.state.socketConnected){
console.log('if condition test',senderid,receiverusername,message,senderusername )
this.socket.emit('send',{senderid,receiverusername,message,senderusername});
this.socket.on(`${receiverusername}`, (d)=>{
if(this.state[`${receiverusername}`]?.length >= 1 ){
let messageList = this.state[`${receiverusername}`]
this.setState({[`${receiverusername}`]:[...messageList,d]})
}
else{
this.setState({[`${receiverusername}`]:[d]})
console.log('else Condition store individual messages', this.state[`${receiverusername}`])
}
}
this.setState( { message:'' })
}
render(){
const { currentUser ,isLoading,userdetails,message,messages} = this.state;
// console.log(messages)
if (isLoading) {
return (<div><p>Loading...</p></div>);
}
if(!currentUser){
return(
<div>
<Redirect to='/login' />
</div>
)
}
else{
return(
<div>
<h1>Messages</h1>
<div>
<p>Users</p>
{' '}
<ul className="collection">
{userdetails.map((element) => {
return(
<div key={element._id}>
<li><Link to={`/dashboard/profile/:${element._id}`}>{element.username}</Link>{' '}<input
type="button"
id={element._id}
value="Message"
onClick={this.showMessageSpace.bind(this,element.username)} ></input></li>
</div>
);
})
}
</ul>
{' '}
</div>
{' '}
<Link to="/dashboard">Dashboard</Link>
{' '}
<div>
{
this.state.show &&
(<div>
<h2>Username : {' '}{this.state.username}</h2>
{' '}
<div>
<h3>Body</h3>
<div>
<ul>
{/* { this.state[`${this.state.username}`]?.map((msg,key) =>{ */}
{this.state.username?.length > 0 && this.state[`${this.state.username}`]?.map((msg,key) =>{
return(<li key={key}>{msg.senderusername}<span>{' '}{msg.message}</span></li>);
})
}
</ul>
</div>
</div>
{' '}
<div>
{' '}
<input
type="text"
name="message"
value={message}
onChange={this.onTextboxChangeMessage}
></input>
<button className='btn btn-info' onClick={(e)=> {this.SendMessage(e,currentUser.user._id,this.state.username,this.state.message,currentUser.user.username)}}>Send</button>
</div>
{' '}
</div>)
}
</div>
</div>
)
}
}
}
server code:
io.on('connection', (socket) => { /* socket object may be used to send specific messages to the new connected client */
console.log('connection established',socket.id);
socket.on('send', (data)=>{
console.log("Receive data from single username",data)
io.emit('message',data)
socket.on('message',data => {
console.log("private")
io.to(data.receiverid).emit('message',data)
})
});
socket.on('disconnected',()=>{
console.log("disconnect")
})
});
This is because you assign another new callback to the socket every time you send a message.
Move this part:
this.socket.on(`${receiverusername}`, (d)=>{
if(this.state[`${receiverusername}`]?.length >= 1 ){
let messageList = this.state[`${receiverusername}`]
this.setState({[`${receiverusername}`]:[...messageList,d]})
} else {
this.setState({[`${receiverusername}`]:[d]})
console.log('else Condition store individual messages',
this.state[`${receiverusername}`])
}
into onconnect callback:
this.socket.on('connect',()=> {
this.setState({ socketConnected : true})
this.socket.on("message", (d)=>{
if(this.state[`${d.receiverusername}`]?.length >= 1 ){
let messageList = this.state[`${d.receiverusername}`]
this.setState({[`${receiverusername}`]:[...messageList,d.content]})
} else {
this.setState({[`${d.receiverusername}`]:[d.content]})
console.log('else Condition store individual messages',
this.state[`${receiverusername}`])
}
})
But in this variant, you don't have receiverusername.
So you should send general "message" event from your server as object containing receiverusername.
The on method is exactly explained in socket.io documentation: https://socket.io/docs/v3/listening-to-events/
And pattern with assigning onmessage callback in the onconnect handler is documented here:
https://socket.io/get-started/chat/#Broadcasting
I have a UserFeed component and EditForm component. As soon as I edit the form, I need to be redirected to the UserFeed and the updated data should be shown on UserFeed(title and description of the post).
So, the flow is like-UserFeed, which list the posts, when click on edit,redirected to EditForm, updates the field, redirected to UserFeed again, but now UserFeed should list the posts with the updated data, not the old one.
In this I'm just redirectin to / just to see if it works. But I need to be redirected to the feed with the updated data.
EditForm
import React, { Component } from "react";
import { connect } from "react-redux";
import { getPost } from "../actions/userActions"
class EditForm extends Component {
constructor() {
super();
this.state = {
title: '',
description: ''
};
handleChange = event => {
const { name, value } = event.target;
this.setState({
[name]: value
})
};
componentDidMount() {
const id = this.props.match.params.id
this.props.dispatch(getPost(id))
}
componentDidUpdate(prevProps) {
if (prevProps.post !== this.props.post) {
this.setState({
title: this.props.post.post.title,
description: this.props.post.post.description
})
}
}
handleSubmit = () => {
const id = this.props.match.params.id
const data = this.state
this.props.dispatch(updatePost(id, data, () => {
this.props.history.push("/")
}))
}
render() {
const { title, description } = this.state
return (
<div>
<input
onChange={this.handleChange}
name="title"
value={title}
className="input"
placeholder="Title"
/>
<textarea
onChange={this.handleChange}
name="description"
value={description}
className="textarea"
></textarea>
<button>Submit</button>
</div>
);
}
}
const mapStateToProps = store => {
return store;
};
export default connect(mapStateToProps)(EditForm)
UserFeed
import React, { Component } from "react"
import { getUserPosts, getCurrentUser } from "../actions/userActions"
import { connect } from "react-redux"
import Cards from "./Cards"
class UserFeed extends Component {
componentDidMount() {
const authToken = localStorage.getItem("authToken")
if (authToken) {
this.props.dispatch(getCurrentUser(authToken))
if (this.props && this.props.userId) {
this.props.dispatch(getUserPosts(this.props.userId))
} else {
return null
}
}
}
render() {
const { isFetchingUserPosts, userPosts } = this.props
return isFetchingUserPosts ? (
<p>Fetching....</p>
) : (
<div>
{userPosts &&
userPosts.map(post => {
return <Cards key={post._id} post={post} />
})}
</div>
)
}
}
const mapStateToPros = state => {
return {
isFetchingUserPosts: state.userPosts.isFetchingUserPosts,
userPosts: state.userPosts.userPosts.userPosts,
userId: state.auth.user._id
}
}
export default connect(mapStateToPros)(UserFeed)
Cards
import React, { Component } from "react"
import { connect } from "react-redux"
import { compose } from "redux"
import { withRouter } from "react-router-dom"
class Cards extends Component {
handleEdit = _id => {
this.props.history.push(`/post/edit/${_id}`)
}
render() {
const { _id, title, description } = this.props.post
return (
<div className="card">
<div className="card-content">
<div className="media">
<div className="media-left">
<figure className="image is-48x48">
<img
src="https://bulma.io/images/placeholders/96x96.png"
alt="Placeholder image"
/>
</figure>
</div>
<div className="media-content" style={{ border: "1px grey" }}>
<p className="title is-5">{title}</p>
<p className="content">{description}</p>
<button
onClick={() => {
this.handleEdit(_id)
}}
className="button is-success"
>
Edit
</button>
</div>
</div>
</div>
</div>
)
}
}
const mapStateToProps = state => {
return {
nothing: "nothing"
}
}
export default compose(withRouter, connect(mapStateToProps))(Cards)
updatePost action
export const updatePost = (id, data, redirect) => {
return async dispatch => {
dispatch( { type: "UPDATING_POST_START" })
try {
const res = await axios.put(`http://localhost:3000/api/v1/posts/${id}/edit`, data)
dispatch({
type: "UPDATING_POST_SUCCESS",
data: res.data
})
redirect()
} catch(error) {
dispatch({
type: "UPDATING_POST_FAILURE",
data: { error: "Something went wrong"}
})
}
}
}
I'm not sure if my action is correct or not.
Here's the updatePost controller.
updatePost: async (req, res, next) => {
try {
const data = {
title: req.body.title,
description: req.body.description
}
const post = await Post.findByIdAndUpdate(req.params.id, data, { new: true })
if (!post) {
return res.status(404).json({ message: "No post found "})
}
return res.status(200).json({ post })
} catch(error) {
return next(error)
}
}
One mistake is that to set the current fields you need to use $set in mongodb , also you want to build the object , for example if req.body does not have title it will generate an error
updatePost: async (req, res, next) => {
try {
const data = {};
if(title) data.title=req.body.title;
if(description) data.description=req.body.description
const post = await Post.findByIdAndUpdate(req.params.id, {$set:data}, { new: true })
if (!post) {
return res.status(404).json({ message: "No post found "})
}
return res.status(200).json({ post })
} catch(error) {
return next(error)
}
}
I'm trying to delete an item after the user clicks on the Delete button. Through the handleDelete function, I am passing an id (idBooks) via axios when the user clicks a book. How do I withdraw it from the click?
Below you will find the React code and then the backend side code in node js.
Frontend
(class extends React.Component {
handleDelete = (e) => {
e.preventDefault();
const { book } = this.props;
axios.delete("http://localhost:8081/delete", book.idBooks )
.then(res => {
console.log(res.data);
}).catch(err => {
console.warn(err.warn);
});
};
render() {
const { book, classes } = this.props;
const token = localStorage.getItem('token');
return(
<Paper className= { classes.marginTopBottom }>
<h2 className={ classes.title }>
{ book.title }
</h2><hr />
<div className= { classes.scrollingDiv }>
<p>
{ book.plot }
</p>
</div>
<hr/>
<div className={ classes.pStyle }>
<p>Publish date:<br /> { new Date(book.publish_date).toLocaleDateString() }</p>
<p>Author:<br /> { book.author }
</p>
<p>Genre:<br /> { book.genre }</p>
</div>
<div>
{ token && (
<Button className={ classes.delete } size="small" onClick={this.handleDelete} type="button" variant="contained" color="primary"
component= {Link} to="delete">
Delete
<DeleteIcon className={ classes.rightIcon } />
</Button>
)}
</div>
</Paper>
)
}
});
Backend
const deleteBook = (req, res) => {
const connection = mysql.createConnection(connectionProperties);
connection.connect();
const query = `DELETE FROM Books WHERE idBooks = ${ req.body.idBooks }`;
connection.query(query, (err, res) => {
if (err) {
res.status(500).send(err);
} else {
res.status(200).send('Book deleted correctly.');
}
});
};
I'd add a prop onDeleteCallback, and on successful delete call that function with deleted book id. In parent component (with all the books are listed) update the state with filtered out books.
I guess passing the parameter might help you fix this issue.
On the delete Button add a parameter to the onClick={()=>this.handleDelete(e,book.idBooks)}
Change the handleDelete function a bit as below
handleDelete = (e,idBooks) => {
e.preventDefault();
axios.delete("http://localhost:8081/delete", idBooks )
.then(res => {
console.log(res.data);
}).catch(err => {
console.warn(err.warn);
});
};
I am able to fetch the data from the db and it is displaying on the inspect element also but it is not displaying on the browser i mean UI.
//storing the data into the posts
this.state = {
displayMenu: false,
posts: [ ]
};
//click function for the drop down and handling the axios.get
Click = event => {
event.preventDefault();
let currentComponent = this;
axios.get(`http://localhost:4000/api/AMS`)
.then(function (response) {
console.log(response);
currentComponent.setState({posts: response.data})
})
.catch(function (error) {
console.log(error);
});
}
//Render method
render() {
// var back = {backgroundSize : 'cover'};
var textStyle = {
position: 'absolute',
top: '50%',
left: '50%'
};
//From here the checking of data is happening, if the data is found inside the posts it will show it on the browser otherwise it will show no posts.
const { posts } = this.state;
const postList = posts.length ? (
posts.map(post => {
// console.log('hi');
return (
<div className="post card" key={post.ID}>
<div className="card-content">
</div>
</div>
)
})
) : ( <div className="center">No posts yet</div>)
//RETURN method
return (
<div>
{/* <Image
style={back} responsive
src={logo}>
</Image> */}
<div style={textStyle} className="dropdown" style = {{background:"red",width:"200px"}} >
<div className="button" onClick={this.showDropdownMenu}> Regions </div>
{ this.state.displayMenu ? (
<ul>
<li><a className="active" href="/AMS" onClick={this.Click}>AMS</a></li>
<li>EMEA</li>
<li>APJ</li>
</ul>
):(null)
}
</div>
//Here i am calling the postList variable
{postList}
{/* {this.state.posts}<br/>
{this.state.pictures} */}
</div>
);
}
}
Click = event => {
event.preventDefault();
let currentComponent = this;
axios.get(`http://localhost:4000/api/AMS`)
.then(function (response) {
console.log(response);
currentComponent.setState({posts: response.data})
})
.catch(function (error) {
console.log(error);
});
}
render() {
// var back = {backgroundSize : 'cover'};
var textStyle = {
position: 'absolute',
top: '50%',
left: '50%'
};
const { posts } = this.state;
const postList = posts.length ? (
posts.map(post => {
// console.log('hi');
return (
<div className="post card" key={post.ID}>
<div className="card-content">
</div>
</div>
)
})
) : ( <div className="center">No posts yet</div>)
The results that i am getting in the inspect element console is like below:
ID: 229, EMAIL: "anuraguk3#gmail.com", ROLE: "BASE", PASSWORD:"$2b$10$ShTWYAtF8M5JLhEm68JqTuMx7P8x6dtOIkNsGz4wE21LY92xGoDCO"
DOM is rendered before getting server response.So, you need to use async-await in this scenario. For your program:-
Click = async(event) => {
event.preventDefault();
let currentComponent = this;
await axios.get(`http://localhost:4000/api/AMS`)
.then(function (response) {
console.log(response);
currentComponent.setState({posts: response.data})
})
.catch(function (error) {
console.log(error);
});
}