mySQL& JavaScript的。似乎无法使用javascript更新我的数据库

时间:2018-02-07 09:33:17

标签: javascript mysql node.js database

我是编程本身的新手,我正在尝试为我的学校项目创建一个食品评论网站。目前,我试图让我的javascript在SQL中将我的数据更新到我的数据库。

我们被教导使用控制器,模型,路由和数据库连接来连接到我们的数据库。我将尝试仅将与我的问题相关的代码放在此处。希望看到正在发生的事情就足够了。

(编辑)我忘了提到我应该使用Postman来测试我的代码确实在改变数据库中的数据!

我仍然不太明白它,但这是我的代码:

我的模型我有2: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;

接下来是我的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;

我的控制器名为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 };

我也有我的路由代码,这个代码叫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 };

最后我的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);
});

1 个答案:

答案 0 :(得分:0)

我认为你的二传手应该是这样的:

//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()
}

如果你使用ORM(用于持久性),你可以拥有save()persist()个东西;除非你需要自己实现save()[或CRUD,如果你需要]方法