繁体   English   中英

我不知道如何在 sequelize (Mac) 中使用 POST 方法

[英]I don't know how to use POST method in sequelize (Mac)

我是一名想了解 javascript 的学生,但英语不太好。正如我之前告诉过你的,我试图制作可编辑的 mypage(profile)。 我使用了 mysql 和续集,我确实获得了制作 mypage 的方法,但不能使用用户可以访问其信息(编辑)的 post 方法。 我只想使用昵称、生日、电话、介绍部分,如果我使用 postman,则显示图片,SyntaxError: Unexpected token / in JSON at Z4757FE07FD492A8BE0EA6A7130D683D6E 型号。这是我的 Mentor 型号。

/* 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,
  });
};

这是我的导师(用户)页面,我想修复它的一部分。

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...
        });
    },
};

这是我的 postman 用于测试

在此处输入图像描述

请帮帮我~

首先,我认为您需要巩固您对 HTTP 方法的了解。 这是一个链接,可以帮助您以最基本的方式理解它。

也就是说,我自然希望你想在你的 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。 它使您的代码更易于阅读(即使对您而言)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM