我不知道如何在 sequelize (Mac) 中使用 POST 方法
I don't know how to use POST method in sequelize (Mac)
我是一个想学习javascript的学生,英语不是很好..
正如我之前告诉你的,我试图制作可编辑的我的页面(个人资料)。
我使用了 mysql 和 sequelize,我确实得到了制作 mypage 的方法,但不能使用 post 方法
用户可以接近他们的信息(编辑)。
我只想使用昵称、生日、phone、介绍部分
如果我使用 postman,请吹掉图片,
SyntaxError:位置 113 处 JSON 中的意外标记 /
出来..
这是我的导师(用户)模型。
/* 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...
});
},
};
这是我的post测试人员
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的学生,英语不是很好.. 正如我之前告诉你的,我试图制作可编辑的我的页面(个人资料)。 我使用了 mysql 和 sequelize,我确实得到了制作 mypage 的方法,但不能使用 post 方法 用户可以接近他们的信息(编辑)。 我只想使用昵称、生日、phone、介绍部分 如果我使用 postman,请吹掉图片, SyntaxError:位置 113 处 JSON 中的意外标记 / 出来.. 这是我的导师(用户)模型。
/* 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...
});
},
};
这是我的post测试人员
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。它使您的代码更易于阅读(甚至对您而言)。