簡體   English   中英

node.js mysql 查詢 product_id 響應 "code": "ER_BAD_FIELD_ERROR" "errno": 1054

[英]node.js mysql query product_id response "code": "ER_BAD_FIELD_ERROR" "errno": 1054

我是 node.js 的新手,並嘗試將它與 mysql 一起使用以從數據庫發出一些簡單的請求。 我試圖在輸入 url http://localhost:8080/api/products/1234567時發出請求,它返回來自 product_id = 1234567的產品的數據。問題是我收到了一個 {" status": {"code":"ER_BAD_FIELD_ERROR", "errno":1054, "sqlState":"42S22", "index":0}} 每次運行時都會出錯。 但是,當我運行http://localhost:8080/api/products 時,它會返回產品表中的 3 列數據。

怎么會出現這個錯誤? 我不明白為什么 /products 有效而 /products/1234567 無效

這是我的代碼:

應用程序.js

var express = require('express');
var bodyParser = require('body-parser');
var dbProducts = require('./dbProducts.js');
var app = express();
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());

var port = process.env.PORT || 8080; // set our port
var router = express.Router();
router.use(function (req, res, next) {
console.log('Incoming request..');
next();
});

// test route to make sure everything is working (accessed at GET http://localhost:8080/api)
router.get('/', function (req, res) {
    res.json({message: 'Welcome to the store api!'});
});
router.route('/products')

// get all the products (accessed at GET http://localhost:8080/api/products)
.get(function (req, res) {
    dbProducts.getProducts(function (err, data) {
        if (data) {
            res.json({
                status: '200',
                items: data
            });
        } else {
            res.json(404, {status: err});
        }
    });
})

數據庫.js

var mysql = require('mysql');
var pool = mysql.createPool({
    host: 'localhost',
    user: 'root',
    port: 3306,
    password: 'password',
    database: 'test'
});
module.exports.pool = pool;

dbProducts.js

var db = require('./db.js');
var getProduct = function getProduct(product_id, callback) {

var get = {id: product_id};
db.pool.getConnection(function (err, connection) {
    // Use the connection
    connection.query('SELECT * FROM PRODUCTS WHERE ? ', get, function (err, results) {
        if (!err) {
            if (results[0] != null) {
                callback(null, results);
            } else {
                callback("Product not found.", null);
            }
        } else {
            callback(err, null);
        }
        //release
        connection.release();
    });

});
}
 var getProducts = function getProducts(callback) {

db.pool.getConnection(function (err, connection) {
    // Use the connection
    connection.query('SELECT * FROM PRODUCTS', function(err, results){
        if (!err) {
            if (results != null) {
                callback(null, results);
            } else {
                callback(err, null);
            }
        } else {
            callback(err, null);
        }
        //release
        connection.release();
    });

});
}

 module.exports.getProduct = getProduct;
 module.exports.getProducts = getProducts;

內部產品表 "items": [ { "product_id": 1234567, "product": "Product 1", "price": 99.99 }, { "product_id": 5555555, "product": "Product 2", "price" : 4.99 }, { "product_id": 8888888, "product": "Product 3", "price": 19.99

嘗試

var get = {"product_id": product_id};

您的表沒有“id”列,它有一個“product_id”列。

異常 1054 指的是未知列異常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM