我是一个想学习 javascript 的学生,而且不太擅长英语..
正如我之前告诉过你的,我试图制作可编辑的 mypage(profile)。
我使用了 mysql 和 sequelize 并且我确实使用了方法来制作我的页面,但不能使用 post 方法
用户可以接近他们的信息(编辑)。
我只想使用昵称、生日、电话、介绍部分
如果我用 postman ,把照片吹掉,
SyntaxError: Unexpected token/in JSON at position 113
出来..
这是我的 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,
});
};
这是我的导师(用户)页面,我想修复它的 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...
});
},
};
这是我的 postman 进行测试enter image description here
请大家帮帮我~
最佳答案
首先,我认为您需要巩固您对 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。它使您的代码更易于阅读(即使对您来说也是如此)。
关于javascript - 我不知道如何在 sequelize (Mac) 中使用 POST 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63057128/