Node.js 使用Post方法的Id对ExpressJS进行续集

Node.js 使用Post方法的Id对ExpressJS进行续集,node.js,express,sequelize.js,Node.js,Express,Sequelize.js,我有一个表单,用于编辑和更新特定Id的记录,并且我能够使用req.params.annotationId访问我路由的GET方法中的Id,但是当我尝试使用带有req.body.annotationId的GET参数的后期版本时,我会得到一个返回值NULL。我还尝试使用req.params.annotationId,它返回了路由的:annotationId占位符。这是因为表单中不存在该字段吗?因为主体解析器查找字段中存在的值,所以哪一个是有意义的 这是POST方法产生的查询: Executing (d

我有一个表单,用于编辑和更新特定Id的记录,并且我能够使用
req.params.annotationId
访问我路由的GET方法中的Id,但是当我尝试使用带有
req.body.annotationId
的GET参数的后期版本时,我会得到一个返回值
NULL
。我还尝试使用
req.params.annotationId
,它返回了路由的
:annotationId
占位符。这是因为表单中不存在该字段吗?因为主体解析器查找字段中存在的值,所以哪一个是有意义的

这是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;
    }
以下是职位申请表:

<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>

注释日期:
更新注释

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
从URL获取annotationId:
/edit/4465465


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
从URL获取annotationId:
/edit/4465465


表单应使用handlebars对象传入所选的当前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>

表单应使用handlebars对象传入所选的当前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>

谢谢你的回答。隐藏字段似乎修复了使用正确的ID值进行的SQL查询,但我在表单提交时遇到了一个无休止的循环。我之所以使用
/app/edit/:annotationId
,是因为它是我为bot发送get和post方法的路由。我把帖子从
/app/edit
发送出去,同样的循环也发生了。知道为什么会这样吗?您需要对Post方法进行响应,如果errorI添加了
},应该添加catch逻辑。catch(函数(error){res.send(error);})
,但是没有触发错误。我注意到我的`console.log(“注释存在”);`未触发console.log。这可能是我的
.update
方法之前存在问题的迹象吗?我解决了问题,我需要我们
.update
而不是
。查找
,然后将
where
子句放在更新方法中谢谢您的回答。隐藏字段似乎修复了使用正确的ID值进行的SQL查询,但我在表单提交时遇到了一个无休止的循环。我之所以使用
/app/edit/:annotationId
,是因为它是我为bot发送get和post方法的路由。我把帖子从
/app/edit
发送出去,同样的循环也发生了。知道为什么会这样吗?您需要对Post方法进行响应,如果errorI添加了
},应该添加catch逻辑。catch(函数(error){res.send(error);})
,但是没有触发错误。我注意到我的`console.log(“注释存在”);`未触发console.log。这是否表明问题存在于我的
.update
方法之前?我发现了问题,我需要使用
.update
而不是
。查找
,然后将
where
子句放在update方法中