Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/471.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript node.js mysql查询产品“U id响应”;代码";:&引用;ER“错误”字段“错误”&引用;“错误”是:1054_Javascript_Mysql_Sql_Node.js - Fatal编程技术网

Javascript node.js mysql查询产品“U id响应”;代码";:&引用;ER“错误”字段“错误”&引用;“错误”是:1054

Javascript node.js mysql查询产品“U id响应”;代码";:&引用;ER“错误”字段“错误”&引用;“错误”是:1054,javascript,mysql,sql,node.js,Javascript,Mysql,Sql,Node.js,我是node.js新手,尝试将其与mysql结合使用,从数据库中发出一些简单的请求。 我试图在输入url时发出请求,它返回来自产品的数据,产品id=1234567。 问题是我收到了一份 {“地位”: {“代码”:“ER_BAD_FIELD_ERROR”, “厄尔诺”:1054, “sqlState”:“42S22”, “索引”:0} 每次运行此操作时都会出错。但是,当我运行它时,它会返回products表中的3列数据 为什么会发生这种错误?我不明白为什么/products起作用和/product

我是node.js新手,尝试将其与mysql结合使用,从数据库中发出一些简单的请求。 我试图在输入url时发出请求,它返回来自产品的数据,产品id=1234567。 问题是我收到了一份 {“地位”: {“代码”:“ER_BAD_FIELD_ERROR”, “厄尔诺”:1054, “sqlState”:“42S22”, “索引”:0} 每次运行此操作时都会出错。但是,当我运行它时,它会返回products表中的3列数据

为什么会发生这种错误?我不明白为什么/products起作用和/products/1234567不起作用

这是我的密码:

app.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});
        }
    });
})
db.js

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;
内部产品表 “项目”:[ { “产品标识”:1234567, “产品”:“产品1”, “价格”:99.99 }, { “产品id”:555, “产品”:“产品2”, “价格”:4.99 }, { “产品id”:88888888, “产品”:“产品3”, “价格”:19.99

var get = {"product_id": product_id};
您的表没有“id”列,它有“product\u id”列


异常1054引用了未知列异常。

它起作用了。{product\u id:product\u id}做什么了?我试图控制台.log(get),它说[object,object]{key:value}是一个javascript对象,构造为键值对。查询函数用对象的元素替换“?”,此处为“get”。这是console.log打印对象的默认行为。请尝试使用console.log(JSON.stringify(obj))打印它
var get = {"product_id": product_id};