[英]mySQL & javascript. Can't seem to UPDATE my database using javascript
I'm fairly new to programming itself and I am currently trying to create a food reviewing website for my school project. 我对编程本身还很陌生,目前正在尝试为我的学校项目创建一个食品评论网站。 Currently, I am stuck trying to get my javascript to update my data to my database in SQL.
目前,我一直在尝试获取JavaScript以将数据更新到SQL中的数据库。
We were taught to use a controller, model, routing and the db-connection to connect to our database. 我们被教导要使用控制器,模型,路由和数据库连接来连接到我们的数据库。 I will try to only put the codes that are relevant to my question in here.
我将尝试仅在此处输入与我的问题相关的代码。 Hopefully, it will be enough to see what is going on.
希望足以看到正在发生的事情。
(edit) I forgot to mention that I am supposed to use Postman to test that my codes are indeed changing the data in my database! (编辑)我忘了提到我应该使用Postman来测试我的代码确实正在更改数据库中的数据!
I still don't quite understand it still but here are my codes: 我仍然不太了解它,但是这是我的代码:
my models I have 2: user.js
is first 我拥有的模型2:
user.js
是user.js
"use strict"
class user {
constructor(userID, username, password, email){
this.userID = userID;
this.username = username;
this.password = password;
this.email = email;
}
//Get
getUserID()
{
return this.userID;
}
getUsername()
{
return this.username;
}
getPassword()
{
return this.password;
}
getEmail()
{
return this.email;
}
//Set
setUsername(username)
{
return this.username;
}
setPassword(password)
{
return this.password;
}
setEmail(email)
{
return this.email;
}
}
module.exports = user;
Next up is my userDB.js
: 接下来是我的
userDB.js
:
"use strict"
var db = require('../db-connection');
class userDB {
changePassword(user, username, password, callback){
var sql = "UPDATE user SET password = ? WHERE username = ?"
console.log("username: " + username + " Password: " + password);
db.query(sql, [user.setPassword(), user.getUsername()] , callback);
}
}
module.exports = userDB;
I have my controller as well named userController.js
: 我的控制器也名为
userController.js
:
"use strict";
const User = require('../models/user');
const UserDB = require('../models/userDB');
var userDB = new UserDB();
function changePassword(request, respond){
var user = new User();
userDB.changePassword(user, request.params.username, request.body.password, function (error, result)
{
if (error) {
respond.json(error);
} else {
respond.json(result);
}
});
}
module.exports = { signUp, deleteAccount, getAllUsers, changePassword };
I have my routing codes as well, this one is called routeUser.js
: 我也有我的路由代码,这个称为
routeUser.js
:
"use strict";
const userController = require('../controllers/userController');
function changePassword(app){
app.route('/userChangePassword/:username')
.put(userController.changePassword);
}
module.exports = { userSignUp, userDeleteAccount, getAllUsers, changePassword };
And finally my server.js
file: 最后是我的
server.js
文件:
"use strict";
const express = require('express');
//Controllers
const routeUser = require('./routes/routeUsers');
const bodyParser = require('body-parser');
var app = express();
var host = "127.0.0.1";
var port = 8080;
var home_file = "/index.html";
app.use(express.static('./public'));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
//User
routeUser.changePassword(app);
function gotoIndex(f, request, respond) {
respond.sendFile(__dirname + f);
}
app.get(home_file, gotoIndex);
// Starts the Web Server
var server = app.listen(port, host, function() {
var host = server.address().address;
var port = server.address().port;
console.log("Example app listening at http://%s:%s", host, port);
});
I think your setters should be something like this : 我认为您的二传手应该是这样的:
//Set
setUsername(username)
{
this.username = username;//then call the save() method if require > kind of this.save()
}
setPassword(password)
{
this.password = password;//then call the save() method if require > kind of this.save()
}
setEmail(email)
{
this.email = email;//then call the save() method if require > kind of this.save()
}
If you use an ORM (for persistence), you can able to have save()
or persist()
stuff; 如果使用ORM(用于持久性),则可以具有
save()
或persist()
东西; unless you need to implement the save() [or CRUD if you need] method yourself 除非您需要自己实现save()[或需要时使用CRUD]方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.