Guest User

Untitled

a guest
Sep 10th, 2018
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. //client
  2.  
  3. import React from 'react';
  4. import $ from 'jquery';
  5. import styles from './photowheel.css';
  6.  
  7. class PhotoWheel extends React.Component {
  8. constructor(props) {
  9. super(props);
  10. this.state = {
  11. photos: [],
  12. restaurant: 2
  13. }
  14. }
  15.  
  16. componentWillMount() {
  17. this.getData();
  18. }
  19.  
  20. getData() {
  21. $.ajax({
  22. method: 'POST',
  23. url: '/photos',
  24. data: {result: this.state.restaurant},
  25. success: (data) => {
  26. this.setState({
  27. photos: data
  28. });
  29. }
  30. });
  31.  
  32. }
  33.  
  34. render() {
  35. return (
  36. <div className="hi">hi
  37. {this.state.photos.map((ele) => {
  38. <img src={ele}/>
  39. })
  40. }
  41.  
  42. </div>
  43. );
  44. }
  45. }
  46.  
  47. export default PhotoWheel;
  48.  
  49.  
  50.  
  51. //server
  52.  
  53. var express = require('express');
  54. var app = express();
  55. var parser = require('body-parser');
  56. var db = require('../database/index.js');
  57.  
  58. app.use(parser.json());
  59. app.use(parser.urlencoded({ extended: true }));
  60. app.use(express.static('./public'));
  61.  
  62.  
  63.  
  64. app.post('/photos', (req, res) => {
  65. console.log(req.body.result);
  66. db.getAllPictures(req.body.result, (data) => {
  67. res.send(data);
  68. });
  69. });
  70.  
  71. var port = process.env.PORT || 3001;
  72.  
  73. app.listen(port, () => console.log("Connected on port 3001"));
  74.  
  75.  
  76. //db
  77. var mysql = require('mysql');
  78.  
  79.  
  80. var db = mysql.createConnection({
  81. multipleStatements: true,
  82. host: 'localhost',
  83. user: 'root',
  84. password: 'nick',
  85. database: 'photos'
  86. });
  87.  
  88.  
  89. db.connect((err) => {
  90. if (err) {
  91. throw err;
  92. }
  93. console.log('connected to db');
  94. });
  95.  
  96. var getAllPictures = function(restaurant, cb) {
  97. db.query(`SELECT url FROM pictures where restaurant = ${restaurant}`, (err, result) => {
  98. if(err) {
  99. console.log(err);
  100. } else {
  101. cb(result);
  102. }
  103. });
  104. }
Add Comment
Please, Sign In to add comment