使用Sequelize.js创建简单的PUT请求时出错

时间:2015-03-04 10:14:49

标签: javascript node.js express sequelize.js

我正在尝试学习sequelize.js,我得到了更新方法的TypeError:

TypeError:无法读取未定义

的属性'params'

我试图获取路线的参数并改变它的值。我试图获取用户名并更改其用户名和密码。这是我的代码:

module.exports = function (sequelize) {
    var model = require("../models/employer_model")(sequelize);
    var Employer = model.Employer;
    return {
        create: function (req, res) {
            if (req.body.username && req.body.password != null) {
                var newEmployer = {
                    username: req.body.username,
                    password: req.body.password
                }

            Employer.create(newEmployer).success(function () {
                res.send(200);
                res.json(req.dataValues);
            });} else {
                res.send(406);
            }
        },
        get: function (req, res) {
            Employer.findAll().success(function (employers) {
                res.send(employers);                
            });
        },
        update: function (req, res) {
            Employer.req.params.id = req.body;
            Employer.save().success(function (employers){
                res.send(200);
                res.json(req.dataValues);
            });
        }
    };
};

这是server.js

// app dependencies
var express = require("express");
var Sequelize = require("sequelize");
var bodyParser = require('body-parser');
var morgan = require('morgan');
var app = express();
var models = require("./models"); 

//sequalize initialization
var sequelize = new Sequelize("postgres://rahul:jobletics@localhost:5432/jobletics");
var employerRoute = require("./routes/employer_route")(sequelize);

//middleware
app.use(bodyParser());
app.use(morgan('dev'));

models.sequelize.sync().success(function (err) {
    console.log('Express server listening on port 5000');
    app.get("/employer", employerRoute.get);
    app.post("/employer", employerRoute.create);
    app.put("/employer/:id", employerRoute.update);
    app.listen(5000);
});

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

我得到的是你试图设置模型的params并保存实例。您应该首先获取要更新/保存的实例。

For updating instance.

如果你想构建一个实例并保存它,你也examples

希望这有帮助。

答案 1 :(得分:0)

Employer.req.params.id = req.body;

我猜这里Employer.req未定义