gpt4 book ai didi

javascript - Node.js 类型错误 : Cannot read property 'host' of undefined

转载 作者:太空宇宙 更新时间:2023-11-04 01:19:00 25 4
gpt4 key购买 nike

我是一个node.js和MySQL初学者,我刚刚开始设置并尝试一些基本代码。当我按照本教程练习“https://medium.com/@ekkodo/node-js-restful-web-api-%E7%AF%84%E4%BE%8B-for-mysql-d8be860594c1”时,它在终端上显示“TypeError:无法读取未定义的属性'主机'”错误,并且无法连接到MySQL。
(程序中的“Product”是我数据库中的表名)

这是我的终端上显示的错误

PS C:\Users\CJSCOPE\Desktop\nodetest\webapi> node app.js
C:\Users\CJSCOPE\Desktop\nodetest\webapi\node_modules\mysql\lib\ConnectionConfig.js:12
this.host = options.host || 'localhost';
^

TypeError: Cannot read property 'host' of undefined
at new ConnectionConfig (C:\Users\CJSCOPE\Desktop\nodetest\webapi\node_modules\mysql\lib\ConnectionConfig.js:12:37)
at Object.createConnection (C:\Users\CJSCOPE\Desktop\nodetest\webapi\node_modules\mysql\index.js:13:34)
at Object.<anonymous> (C:\Users\CJSCOPE\Desktop\nodetest\webapi\models\product.js:4:24)
at Module._compile (internal/modules/cjs/loader.js:1128:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1167:10)
at Module.load (internal/modules/cjs/loader.js:983:32)
at Function.Module._load (internal/modules/cjs/loader.js:891:14)
at Module.require (internal/modules/cjs/loader.js:1023:19)
at require (internal/modules/cjs/helpers.js:72:18)
at Object.<anonymous> (C:\Users\CJSCOPE\Desktop\nodetest\webapi\routes\product.js:2:15)

这是我的node.js代码

app.js

var bodyparser = require('body-parser'); 
var express = require('express');

var config = require('./config');
var functions = require('./function');
var product =require('./routes/product');

var app=express();

req.body
app.use(bodyparser.urlencoded({ extended:false }));
app.use(bodyparser.json());

app.use(functions.passwordCrypto);
app.use('./product',product);

app.listen(config.port, function(){
console.log('app listening on port'+ config.port + '!');
});

product.js(模型)

var mysql =require('mysql');
var config =require('../config');

var connection = mysql.createConnection(config.db);
var sql = '';

module.exports = {
item:function(req,callback){
sql = 'SELECT * FROM product';
return connection.query(sql,callback);
},
item:function(req,callback){
sql = mysql.format('SELECT * FROM product WHERE id= ?',[req.params.id]);
return connection.query(sql,callback);
},
add:function(req,callback){
console.log('req.body',req.body);
sql = mysql.format('INSERT INTO product SET ?',req.body);
return connection.query(sql,callback);
},
delete:function(req,callback){
sql = mysql.format('DELETE * FROM product WHERE id= ?',[req.params.id]);
return connection.query(sql,callback);
},
put:function(req,callback){
connection.beginTransaction(function(err){
if(err) throw err;

sql=mysql.format('DELETE * FROM product WHERE id= ?',[req.params.id]);

connection.query(sql,function(err,results,fields){
if(results.affectedRows){
req.body.id=req.params.id;
sql=mysql.format('INSERT INTO product SET ?',req.body);

connection.query(sql,function(err,results,fields){
if(err){
connection.rollback(function(){
callback(err, 400);
});
}else{
connection.commit(function(err){
if(err) callback(err,400);
callback(err,200);
});
}
});
}else{
callback(err,410);
}
});
});
},

patch:function(req,callback){
sql = mysql.format('UPDATE product SET ? WHERE id = ? ',[req.body, req.params.id]);
return connection.query(sql,callback);
}
}

product.js(路由)

var express = require('express');
var product = require('../models/product');

var router=express.Router();

router.route('/')
.get(function (req,res){
product.items(req, function(err, results, fields){
if(err){
res.sendStatus(500);
return console.error(err);
}

if(!results.length){
res.sendStatus(404);
return;
}

res.json(results);
});
})
.post(function(req,res){
product.add(req, function(err,results,fields){
if(err){
res.sendStatus(500);
return console.error(err);
}
res.status(201).json(results.insertId);
});
});

router.route('./id')
.get(function(req,res){
product.item(req,function(err,results,fields){
if(err){
res.sendStatus(500);
return console.error(err);
}

if(!results.length){
res.sendStatus(404);
return;
}

res.json(results);
});
})
.delete(function (req,res){
product.delete(req,function(err,results,fields){
if(err){
res.sendStatus(500);
return console.error(err);
}

if(!results.affectedRows){
res.sendStatus(410);
return;
}

res.sendStatus(204);
});
})
.put(function(req,res){
product.put(req,function(err,results){
if(err){
res.sendStatus(500);
return console.error(err);
}

if(results === 410){
res.sendStatus(410);
return;
}

product.item(req,function(err,results,fields){
res.json(results);
});
});
})
.patch(function(req,res){
product.patch(req,function(err,results){
if(err){
res.sendStatus(500);
return console.error(err);
}

if(!results.affectedRows){
res.sendStatus(410);
return;
}

req.body.id=req.params.id;
res.json([req.body]);
});
});
module.exports = router;

function.js

var crypto = require('crypto'); 
var config = require('./config');

module.exports = {
passwordCrypto: function(req,res,next){
if(req.body.password){
req.body.password=crypto.createHash('md5')
.update(req.body.password+config.salt)
.digest('hex');
}
next();
}
};

config.js

module.export = {
db:{
host:'127.0.0.1',
user:'root',
password:'1234',
database:'shop',
port:3306
},
port:3300
}

最佳答案

这就是您需要通过传递所有必需的 pram 来创建 mysql 连接对象的方式,示例:

    var config =require('../config');
const connection = mysql.createConnection({
host: config.db.host,
user: config.db.user,
password: config.db.password,
database: config.db.database
});

确保 var config =require('../config'); 的路径正确,并且您可以通过悬停该文件来读取该文件。

同时将 config.js 文件中的 module.export = { 更改为 module.exports = {

关于javascript - Node.js 类型错误 : Cannot read property 'host' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59948766/

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