简体   繁体   中英

RESTFul APIs with Express and MySQL

been trying to figure out why does the code not output the error, even when in postman, it shows that it doesn't work. my app.get works just find, however, my app.delete and app.put doesn't seem to work but there's not output to tell me what the code went wrong. Thanks for any advice and opinions! Postman showing the error

在此处输入图片说明

This is my sql code:

CREATE DATABASE  IF NOT EXISTS `food_db`;
USE `food_db`;
DROP TABLE IF EXISTS `foodmenu`;

CREATE TABLE foodmenu (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
foodName VARCHAR(50) NOT NULL,
foodDescription VARCHAR(254) NOT NULL,
price DECIMAL(4,2) NOT NULL,
reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

INSERT INTO foodmenu (foodName, foodDescription, price) values 
("Fillet o Dory", "Fillet o Dory, our delicious fish sandwich made with wild-caught fish, cheese and tartar sauce", 6.99), 
("McSpoosy", "Some kind of garlic, spicy goodness that's a hot selling item since 2001", 6.66),
("McDrumlets", "If drumlet’s your thing, our McDrumlets will send you right over the mountain. Coated with a light batter, we guarantee you’ll be Shiok ah!", 4.99),
("Double Chili Burger", "Chili, Spice, and everything nice. These were the ingredients chosen to create the perfect the Double Chili Burger", 8.99);

select * from foodMenu;
var express=require('express');
var app=express();

var bodyParser=require('body-parser');
var urlencodedParser=bodyParser.urlencoded({extended:false});

app.use(bodyParser.json()); //parse appilcation/json data
app.use(urlencodedParser);

const foodMenu = require("../model/foodMenu.js")

app.get("/foodMenu/", (req, res, next) => {
    foodMenu.findAll((error,result)=>{
        console.log(req.body);
        if (error) {
          console.log(error);
          res.status(500).send(console.log("Internal Server Error"));
          console.log('"Result"' + ":" + '"Internal Error"')
        };
        res.status(200).send(result);
    })
  });   

  app.put("/foodMenu/:foodID",(req,res,next)=>{
    var foodid = req.params.foodID;
    console.log(req.body)
     foodMenu.edit(foodid,req.body,(error,result)=>{
         if(error){
             console.log(error);
             res.status(500).send('{"Results":Internal Error}');
             return;
            }
            else 
            console.log(result)
            res.status(200).send('{Affected Rows:${result}}')

    })

  })
app.delete("/foodMenu/:foodID",(req,res,next)=>{
  var foodid = req.params.foodID;
  foodMenu.delete(foodid,(error,result)=>{
    if(error){
      console.log(error)
      res.status(500).send('{"Results":Internal Error}')
      return;
    }
    console.log(result)
    res.status(201).send('{Affected Rows:${result}}')
  })
})

module.exports=app;

//seperate file (foodMenu.js)
var db = require('./databaseConfig.js');
 db = db.getConnection()

var modules = {
    findAll: function(callback){
        const  findAllmenuQuery = "SELECT foodName, foodDescription, price FROM foodmenu";
        db.query(findAllmenuQuery,(error,result)=>{
            if(error){
                callback(error,null);
                return;
            }   
            callback(null,result);
        })
    },

    edit: function(foodID,data,callback){
        var editQuery = "update foodmenu set foodName = ?,foodDescription = ?, price = ?  where id = ? " // variable insertQuery, enables you to parse in name, course, code 
        console.log(data)
        db.query(editQuery,[data.foodName,data.foodDescription,data.price,foodID],(error,result)=>{
            if(error){
                callback(error,null);
                return;
            }
            callback(null,result.affectedRows);
        })
    },

    delete: function(foodID,callback){
        var deleteQuery = " delete from foodmenu where id = ?"
        db.query(deleteQuery,[foodID],(error,result)=>{
            if(error){
                callback(error,null);
                return;
            }
            callback(null,result);
        })

    }

}

module.exports = modules

I think you need just to remove : in uri

So try this localhost:3000/foodMenu/3 directly in your postman

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM