gpt4 book ai didi

javascript - 无法触发 REST API 将记录添加到 MySQL DB

转载 作者:行者123 更新时间:2023-11-29 15:41:30 24 4
gpt4 key购买 nike

我在使用 Node.JS 和 Postman 向 MYSQL 插入数据时遇到问题

我创建了 3 个文件 - db.js、server_db.js、server.js。 Server.JS 是当我们执行 GET/POST 时被调用的主文件。我无法在 MYSQL DB 中插入任何记录。我在这里遵循 promise 代码 - 拒绝/响应。

  1. db.js-

    var mysql = require("mysql");
    var db_config = {
    主机:'127.0.0.1',
    用户:'根',
    密码 : '*****',
    数据库:'网络应用程序'
    };
    变量连接;
    console.log("db.js 内部 1")
    连接 = mysql.createConnection(db_config);
    连接.connect(函数(错误){
    if(err) 抛出错误
    console.log('您现在已连接..');
    });
    module.exports = 连接;

server_db.js(该文件包含要触发的SQL查询并使用此处的Promises来解析和拒绝)

var db = require('../db.js');
save_user_information = (data) => new Promise((resolve, reject)=>{
db.query('INSERT INTO lottery_information SET ?',data,function(err,results,fiels){
if(err){
reject('could not insert into lottery information');
}
resolve('Successful');
});
});
module_exports = {save_user_information};

服务器.js

从我们调用 API 的地方

const express = require('express');
const app = express();
const bodyParser = require("body-parser");
const save_user_information = require('./models/server_db');
app.use(bodyParser.json());
app.post('/', async function(req, res){
var email = req.body.email;
var amount = req.body.amount;
if(amount <= 1){
return_info = {};
return_info.error = true;
return_info.message = "The amount should be greater than 1";
return res.send(return_info);
}
var result = await save_user_information({"amount" : amount, "email" : email});
res.send(result);
});
app.listen(3000, ()=>{
console.log('Server is running on port 3000');
});

我收到错误 -

(node:17568) UnhandledPromiseRejectionWarning: TypeError: save_user_information is not a function
at C:\GIT\Web2.0\server.js:41:23
at Layer.handle [as handle_request] (C:\GIT\Web2.0\node_modules\express\lib\router\layer.js:95:5)
at next (C:\GIT\Web2.0\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\GIT\Web2.0\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\GIT\Web2.0\node_modules\express\lib\router\layer.js:95:5)
at C:\GIT\Web2.0\node_modules\express\lib\router\index.js:281:22
at Function.process_params (C:\GIT\Web2.0\node_modules\express\lib\router\index.js:335:12)
at next (C:\GIT\Web2.0\node_modules\express\lib\router\index.js:275:10)
at C:\GIT\Web2.0\node_modules\body-parser\lib\read.js:130:5
at invokeCallback (C:\GIT\Web2.0\node_modules\raw-body\index.js:224:16)
(node:17568) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:17568) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

因此,“save_user_information”似乎不被视为功能。

请您指出我哪里出错了。

最佳答案

首先,要导出模块,您必须使用点,而不是下划线。然后,当您导出命名模块时,您应该将其导入为命名模块。

关于javascript - 无法触发 REST API 将记录添加到 MySQL DB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57586151/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com