【问题标题】:I don't know how to use POST method in sequelize (Mac)我不知道如何在 sequelize (Mac) 中使用 POST 方法
【发布时间】:2020-11-13 08:22:41
【问题描述】:

我是一名想学习 javascript 的学生,但英语不太好.. 正如我之前告诉过你的,我试图制作可编辑的 mypage(profile)。 我使用了 mysql 和 sequelize,我确实得到了制作 mypage 的方法,但不能使用 post 方法 用户可以接近他们的信息(编辑)。 我只想使用昵称、生日、电话、介绍部分 如果我用邮递员,把照片吹掉, SyntaxError:意外的令牌/在 JSON 中的位置 113 出来.. 这是我的导师(用户)模型。

/* jshint indent: 2 */

module.exports = function (sequelize, DataTypes) {
  return sequelize.define('Mentors', {
    id: {
      autoIncrement: true,
      type: DataTypes.INTEGER,
      allowNull: false,
      primaryKey: true,
    },
    mentor_name: {
      type: DataTypes.STRING(255),
      allowNull: true,
    },
    nickname: {
      type: DataTypes.STRING(255),
      allowNull: true,
    },
    email: {
      type: DataTypes.STRING(255),
      allowNull: true,
    },
    password: {
      type: DataTypes.STRING(255),
      allowNull: true,
    },
    sex: {
      type: DataTypes.STRING(255),
      allowNull: true,
    },
    phone: {
      type: DataTypes.STRING(255),
      allowNull: true,
    },
    birthday: {
      type: DataTypes.STRING(255),
      allowNull: true,
    },
    certification_path: {
      type: DataTypes.STRING(255),
      allowNull: true,
    },
    intro: {
      type: DataTypes.STRING(255),
      allowNull: true,
    },
  }, {
    sequelize,
    tableName: 'Mentors',
    timestamps: false,
  });
};

这是我的导师(用户)页面,我想修复其中的 POST 部分。

const db = require('../../../models');

const { Mentors } = db;

module.exports = {
    get: (req, res) => {
        if (req.params.id) {
            Mentors
            .findOne({
                where: {
                    id: req.params.id,
                },
            })
            .then((result) => {
                if (result) {
                    res.status(200).json(result);
                } else {
                    res.status(409).send('Wrong Access');
                }
            })
            .catch((err) => {
                res.status(500).send(err);
            });
        }
    },
    post: (req, res) => {
        Mentors
        .findOne({
            where: {
                id: req.params.id,
            },
        })
        .then((result) => {
            if (result) {
                res.status(200).json(result);
            } else {
                res.status(409).send('Wrong Access');
            }
        })
        .catch((err) => {
            res.status(500).send(err); // help me...
        });
    },
};

这是我用于测试的邮递员

enter image description here

请帮帮我~

【问题讨论】:

    标签: javascript mysql post sequelize.js postman


    【解决方案1】:

    首先,我认为您需要巩固您对 HTTP 方法的了解。 Here is a link 帮助你最基础的理解。

    也就是说,我自然希望您希望在您的 POST 中创建一位导师。下面的代码看起来像您可能计划在 post 方法中执行的操作。

    const post = (req, res) => {
            Mentors
            .findOne({
                where: {
                    id: req.params.id,
                },
            })
            .then((result) => {
                if (result) {
                    res.status(400).json({
                      error: true,
                      message: 'mentor account already exists',
                    });
                } else {
                    //Get your request body from req.body
                    Mentor.create({
                      nickname: req.body.nickname //the name given to it on the frontend
                      // include the other fields you need to create the mentor
                    }).then((mentor) => res.json({ error: false, data: mentor })
                }
            })
            .catch((err) => {
                res.status(500).send(err); // help me...
            });
        }

    我还强烈建议您学习使用 ES6 async/await。它使您的代码更易于阅读(即使对您而言也是如此)。

    【讨论】:

    • 非常感谢您的评论,我会阅读您的网址链接。谢谢你帮助我
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-14
    • 2011-04-02
    • 1970-01-01
    相关资源
    最近更新 更多