node.js - 使用 Id 作为 Post 方法的 Sequelize ExpressJS

标签 node.js express sequelize.js

我有一个表单,用于编辑和更新特定 Id 的记录,并且我可以使用 req.params.annotationId 在我的路线的 GET 方法中访问 Id,但是当我尝试使用通过 req.body.annotationId 获取参数的 POST 版本时,我得到的返回值是 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;
    }

以下是 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>

最佳答案

req.body.annotationId从数据中获取注释ID,格式如下:

<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 获取注释ID:/edit/4465465

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

关于node.js - 使用 Id 作为 Post 方法的 Sequelize ExpressJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34486935/

相关文章:

java - 由 NodeJS 生成的 Java 进程中网络接口(interface)的 IP 地址

node.js - 在我将 session 设置为存储在 db 中之后,它在注册之前会有一个延迟。 Node/ express / express session ,

javascript - 在 NodeJS ExpressJS 中从 req.body 中删除一个变量

javascript - Node.js for 循环 : strange behavior

node.js - 如何配置express.js/jade来处理html文件?

node.js - 如何使用 ExpressJS 在 XML 中响应?

node.js - 无法将 Postgres 与 NodeJS 连接(Docker Compose)

model - 自定义 .find() 绑定(bind)到 Sequelize 模型

javascript - 如何在模型和 Controller 之间使用sequelize发送查询结果

node.js - Node + mongo-native : how to find and update value in collection?