javascript - node.js mysql query product_id response "code": "ER_BAD_FIELD_ERROR" "

I am new to node.js and trying to use it with mysql to make some simple request from the database. I a

I am new to node.js and trying to use it with mysql to make some simple request from the database. I am trying to make a request when typed into a url http://localhost:8080/api/products/1234567 and it returns the data from the products take with the product_id = 1234567. The issue is that i am recieving an {"status": {"code":"ER_BAD_FIELD_ERROR", "errno":1054, "sqlState":"42S22", "index":0}} error every time i run this. However when I run http://localhost:8080/api/products it returns the 3 columns of data that I have in the products table.

How e this error is happening? I dont understand why /products works and /products/1234567 does not work

here is my code:

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('Ining 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: 'Wele 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

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;

inside products table "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

I am new to node.js and trying to use it with mysql to make some simple request from the database. I am trying to make a request when typed into a url http://localhost:8080/api/products/1234567 and it returns the data from the products take with the product_id = 1234567. The issue is that i am recieving an {"status": {"code":"ER_BAD_FIELD_ERROR", "errno":1054, "sqlState":"42S22", "index":0}} error every time i run this. However when I run http://localhost:8080/api/products it returns the 3 columns of data that I have in the products table.

How e this error is happening? I dont understand why /products works and /products/1234567 does not work

here is my code:

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('Ining 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: 'Wele 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

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;

inside products table "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

Share Improve this question asked Oct 31, 2015 at 18:09 user3464613user3464613 1151 gold badge3 silver badges9 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 1

Try

var get = {"product_id": product_id};

Your table doesnt have an 'id' column, it has a 'product_id' column.

The exception 1054 refers to unknown column exception.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745664582a4639046.html

相关推荐

  • 精品网络时代:联通AS9929与10099的强强联合

    中国联通的网络架构犹如一座精心设计的立交桥系统,由AS4837、AS9929和AS10099三张骨干网共同构建。这三张网络各司其职又相互配合,形成了联通独具特色的网络服务体系。联通AS4837、AS9929和AS10099线路介绍一、线路组

    1小时前
    20
  • 如何增加 Elasticsearch 中的主分片数量

    要增加现有索引的主分片数量,直接修改是不可能的。因此,如果你想增加主分片的数量,必须重新创建索引。通常有两种方法:_reindex API 和 _split API。在这两种方法中,_split API 通常比 _reindex API 更

    1小时前
    20
  • Kibana Alerting: 突破扩展性限制,实现50倍规模提升

    在过去的几年里,Kibana告警一直是许多大型组织的首选监控解决方案。随着使用的不断增加,用户创建的告警规则数量也在增长。越来越多的组织依赖Kibana进行大规模告警,我们看到了提高效率和确保未来工作负载性能的机会。在Kibana 8.16

    1小时前
    20
  • AI驱动的幼儿跌倒检测——视频安全系统的技术解析

    幼儿跌倒检测系统利用AI视频技术,结合人体姿态识别和实时报警功能,提供了一种智能化解决方案。本文将深入剖析其技术架构、模块实现与应用场景,探讨开源技术如何提升幼儿园安全管理。技术架构与实现项目背景幼儿在活动区域(如教室、操场)易发生跌倒,需

    1小时前
    00
  • 打破常规!支付宝小程序地图功能开发实用技巧,拓展业务场景

    打破常规!支付宝小程序地图功能开发实用技巧,拓展业务场景嘿,各位开发者小伙伴们

    1小时前
    00
  • 文章降 AI 痕迹方法与工具速览

    文章降AI的方法和工具汇总‌如下: 这几天又认真研究类了一下,想让 AI 生成的文章更自然,摆脱程式化痕迹,可尝试以下方法。借助 GPT、文字滚筒鸭,朱雀大模型检测器、豆包、kimi 等大模型,输入文本后,它们能通过调整结构、替换同义词等操

    1小时前
    00
  • 利用小提琴图探索帕尔默企鹅数据

    利用小提琴图探索帕尔默企鹅数据代码语言:javascript代码运行次数:0运行复制import numpy as npimport matplotlib.pyplot as pltimport scipy.stats as stfro

    1小时前
    00
  • 用安信可Ai

    以下作品由安信可社区用户业余菜狗制作前言自从接触智能家居之后,笔者就变得很依赖智能家居(绝对不是懒!)比如卧室灯,就在进门的地方,进门开灯很方便,但是晚上睡觉关灯就很不方便。之前是买了一款Wi-Fi灯,是用手机APP操作,刚开始用的时候感觉

    1小时前
    00
  • Claude 不是只能聊天:我用这5个 MCP 工具,让它像程序员一样工作

    最近在折腾 Claude,原本只是拿来写点文案、回答技术问题。但渐渐地我有点不满足了——AI 不能只会说话,它得“动”起来。于是,我遇见了 MCP Server,彻底打开了新世界大门。

    1小时前
    00
  • UMIT:统一多模态多任务视觉

    随着深度学习的迅速发展,尤其是在医学影像分析领域的应用,越来越多的视觉-语言模型(VLMs)被广泛应用于解决复杂的健康和生物医学挑战。然而,现有研究主要集中在特定任务或单一模态上,这限制了它们在多种医学场景中的适用性和泛化能力。为了解决这

    1小时前
    00
  • PackML over OPC UA

    在当今数字化转型的浪潮中,制造业正面临着前所未有的挑战与机遇。如何实现设备之间的高效通信与集成,成为提升生产效率、降低成本的关键。OPC UA(OPC Unified Architecture)与PackML(Packaging Machi

    55分钟前
    00
  • 万字图解 Java 并发框架:ForkJoin、CountDownLatch、Semaphore、CyclicBarrier

    本文图多,内容硬核,建议收藏。在第一章节《1.6w 字图解 Java 并发:多线程挑战、线程状态和通信、死锁;AQS、ReentrantLock、Condition 使用和原理》,我们开启了 Java 高并发系列的学习,透彻理解 Java

    53分钟前
    00
  • 10个 DeepSeek 神级提示词,建议收藏!

    在当下人工智能飞速发展的时代,DeepSeek 作为一款功能强大的 AI 工具,能够帮助我们实现各种创意和需求。然而,要充分发挥它的潜力,掌握一些巧妙的提示词至关重要。今天,就为大家精心整理了 15 个 DeepSeek 神级提示词,涵盖多

    50分钟前
    00
  • 初始JESD204B高速接口协议(JESD204B一)

    01、对比LVDS与JESD204JESD204B是逻辑器件和高速ADCDAC通信的一个串行接口协议,在此之前,ADCDAC与逻辑器件交互的接口大致分为如下几种。低速串行接口(I2C、SPI)、低速并行接口(包含时钟信号和并行数据信号,

    48分钟前
    00
  • 国产之光!!让你的Docker管理更优雅!

    大家好,我是热爱开源的了不起!我们都知道,Docker是个好东西,能帮我们把应用打包成容器,方便部署和管理。但问题来了,Docker的命令行操作对新手来说有点复杂,一不小心就容易出错。而且,有时候我们只是想简单地管理一下容器,却得记住一堆命

    43分钟前
    00
  • 国产车载通信测试方案:车规级CAN SIC芯片测试技术解析

    随着智能网联汽车的快速发展,车辆内部电子控制单元(ECU)数量激增,动力总成、高级驾驶辅助系统(ADAS)、车身控制等功能对车载通信网络的稳定性与速率提出了更高要求。传统CAN FD总线在复杂拓扑中面临信号振铃、通信速率受限(实际速率通常低

    31分钟前
    00
  • 如何快速判断 Flutter 库是否需要适配鸿蒙?纯 Dart 库无需适配!

    在鸿蒙开发中,选择合适的 Flutter 库至关重要。纯 Dart 库因其跨平台特性,无需适配即可直接使用。但对于新手来说,如何判断一个库是否为纯 Dart 库呢?本文将为你提供清晰的判断方法和实用技巧。一、检查 pubspec.yaml

    19分钟前
    00
  • MongoDB “升级项目” 大型连续剧(2)

    上期写的是非必要不升级,想到这个事情,有一些事情的仔细琢磨琢磨,为什么数据库升级的事情在很多公司都是一个困扰,从一个技术人的观点,升级是一件好事,功能提升了,性能提升了,开发效率和一些数据库使用的痛点也被解决了,为什么就不愿意升级呢?如果只

    17分钟前
    00
  • 推荐一个轻量级的监控平台并且支持移动端

    简介XUGOU 是基于Cloudflare构建的轻量化监控平台,专精于系统资源监控与可视化状态页面服务。该平台提供英文简体中文双语支持,满足全球化部署需求。面向开发者及中小团队,项目致力于提供高可用性的监控解决方案。核心功能与实现平台功能

    9分钟前
    00
  • maxwell遇到的一则问题

    结论和原因maxwell的元数据库里面没有存储全部的schema数据(就是少数据了),导致相关表的DDL校验失败。PS:我这里maxwell的作用只是采集库表修改情况的统计粗粒度指标,因为之前maxwell在运行报错的时候,直接修改了pos

    1分钟前
    00

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信