Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
224 views
in Technique[技术] by (71.8m points)

javascript - 带有Express和MySQL的RESTFul API(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.

(我的app.get可以找到,但是我的app.delete和app.put似乎不起作用,但是没有输出可以告诉我代码出了什么问题。)

Thanks for any advice and opinions!

(感谢您的任何建议和意见!)

Postman showing the error

(邮递员显示错误)

在此处输入图片说明

This is my sql code:

(这是我的SQL代码:)

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
  ask by Ng Aouang translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I think you need just to remove : in uri

(我认为您只需要删除:uri)

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

(因此,直接在邮递员中尝试使用localhost:3000/foodMenu/3)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...