使用Id作为Post方法的Sequelize ExpressJS

时间:2015-12-28 02:17:28

标签: node.js express sequelize.js

我有一个用于编辑和更新特定身份证记录的表单,我可以使用req.params.annotationId访问我的GET方法中的ID,但是当我尝试使用时使用req.body.annotationId获取参数的POST版本我得到的值为NULL。我还尝试使用req.params.annotationId并返回路由的:annotationId占位符。这是因为该字段不存在于表单中吗?这有意义,因为body-parser会查找字段中存在的值?

这是来自POST方法的结果查询:

Executing (default): SELECT `annotation_id` AS `annotationId`, `annotation_date` AS `annotationDate`,`user_id` AS `userId`, `createdAt`, `updatedAt`, `userUserId` FROM `annotation` AS `annotation` WHERE `annotation`.`user_id` = 1 AND `annotation`.`annotation_id` = NULL LIMIT 1;

这是我的路线:

appRoutes.route('/edit/:annotationId')

    .get(function(req, res){
        console.log('This is the url path ' + req.originalUrl);

        console.log(req.params.annotationId);

        models.Annotation.find({
                where: {
                    userId: req.user.user_id,
                    annotationId: req.params.annotationId
                },attributes: ['annotationId', 'annotationDate']
            }).then(function(annotation){
                res.render('pages/annotation-edit.hbs',{
                    annotation: annotation,
                    user: req.user,
                    editMode: req.originalUrl
                });
        })          
    })


    .post(function(req, res){

        console.log("POST method triggered");

        console.log(req.params.annotationId);

        models.Annotation.find({
            where: {
                    userId: req.user.user_id,
                    annotationId: req.body.annotationId
            }
        }).then(function(annotation){
                if (annotation) {
                    console.log("Annotation exists");
                    annotation.update({
                        annotationDate: req.body.annotationDate,
                        userId: req.user.user_id
                    }).success(function() {
                        console.log("Annotation Updated");
                    });
                }
            })
        });

这是我的注释模型:

  module.exports = function(sequelize, DataTypes) {

    var Annotation = sequelize.define('annotation', {
        annotationId: {
            type: DataTypes.INTEGER,
            field: 'annotation_id',
            autoIncrement: true,
            primaryKey: true
        },
        annotationDate: {
            type: DataTypes.DATE,
            field: 'annotation_date'
        },
        userId: {
            type: DataTypes.STRING,
            field: 'user_id'
        }
    },

     {
        freezeTableName: true,
        },
        classMethods: {
            associate: function(db) {
                Annotation.belongsTo(db.User)
            }
        }
    });
        return Annotation;
    }

以下是POST请求的表单:

<div class="row">
    <div class="col-md-8 col-md-offset-2">
        <div class="annotation-form">
            <form action="/app/edit/:annotationId" method="post">
                <div class="annotation-form-header">
                    <img class="user-image" src="http://placehold.it/80x80" alt="Generic placeholder image">
                    <label for="annotation-date">Annotation Date:</label>
                    <input type="date" name="annotationDate" id="annotation-form-date" value="{{annotation.annotationDate}}">
                </div>
                <button type="submit" id="create-annotation-button">Update Annotation</button>
            </form>

2 个答案:

答案 0 :(得分:0)

req.body.annotationId使用以下格式的数据获取annotationID:

<form action="/app/edit" method="post">
                <input name="annotationId" type="hidden" value="121313">
                <div class="annotation-form-header">
                    <img class="user-image" src="http://placehold.it/80x80" alt="Generic placeholder image">
                    <label for="annotation-date">Annotation Date:</label>
                    <input type="date" name="annotationDate" id="annotation-form-date" value="{{annotation.annotationDate}}">
                </div>
                <button type="submit" id="create-annotation-button">Update Annotation</button>
            </form>

```

req.params.annotationId从网址获取annotationID:/edit/4465465

<form action="/app/edit/:annotationId" method="post">&lt; - 无效网址

答案 1 :(得分:0)

表单应使用把手对象传递当前选择的Id,如

<form action="/app/edit/{{annotation.annotationId}}" method="post">
                <input name="annotationId" type="hidden" value="121313">
                <div class="annotation-form-header">
                    <img class="user-image" src="http://placehold.it/80x80" alt="Generic placeholder image">
                    <label for="annotation-date">Annotation Date:</label>
                    <input type="date" name="annotationDate" id="annotation-form-date" value="{{annotation.annotationDate}}">
                </div>
                <button type="submit" id="create-annotation-button">Update Annotation</button>
            </form>

然后路线应从.find更改为.update

.post(function(req, res){

        console.log("POST method triggered");

        console.log(req.params.annotationId);

        models.Annotation.update({
            annotationId: req.body.annotationId,
            annotationDate: req.body.annotationDate,
        },{where:{
            userId: req.user.user_id,
            annotationId: req.body.annotationId
        }}).then(function(){
            console.log("Annotation was Updated");
            res.redirect('/app');
        });
    });
相关问题