如何通过在 sequelize orm 中迁移 post 数据从表单到 postgres table
how to post data from form to postgres table through migration in sequelize orm
必须post数据从FORMhtml到POSTGREStable通过migration sequelize orm
我通过迁移创建了 table....但无法将表单数据插入 table
form.html
<form action="http://localhost:3000/register">
<fieldset class="p-4">
<input type="email" placeholder="Email*" id = "email" class="border p-3 w-100 my-2">
<input type="password" placeholder="Password*" id = "password" class="border p-3 w-100 my-2">
<input type="password" placeholder="Confirm Password*" id = "con_password" class="border p-3 w-100 my-2">
<div class="loggedin-forgot d-inline-flex my-3">
<input type="checkbox" id="registering" class="mt-1">
<label for="registering" class="px-2">By registering, you accept our <a class="text-primary font-weight-bold" href="terms-condition.html">Terms & Conditions</a></label>
</div>
<button type="submit" class="d-block py-3 px-4 bg-primary text-white border-0 rounded font-weight-bold" onclick="registrationform()">Register Now</button>
</fieldset>
</form>
下面是我的种子文件代码:
seeds/20210823074536-Users.js
module.exports = {
up: async (queryInterface, Sequelize) => {
router.post('/register',async(req,res)=>{
const { email,password: plainTextPassword } = req.body
const password = await bcrypt.hash(plainTextPassword,10);
try{
await queryInterface.bulkInsert('RegisterUsers', [{
email: email,
password: password ,
con_password: password
}])
}
catch(error) {
console.log(error)
}
});
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('RegisterUsers', null, {});
迁移/
20210818092900-创建-注册-user.js
'use strict';
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.createTable('RegisterUsers', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
email: {
type: Sequelize.STRING
},
password: {
type: Sequelize.STRING
},
con_password: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: async (queryInterface, Sequelize) => {
await queryInterface.dropTable('RegisterUsers');
}
};
npx sequelize-cli seed:generate --name demo-user
此命令将在 seeders
文件夹中创建种子文件,类似于
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.bulkInsert('Users', [{
firstName: 'John',
lastName: 'Doe',
email: 'example@example.com',
createdAt: new Date(),
updatedAt: new Date()
}]);
},
down: (queryInterface, Sequelize) => {
return queryInterface.bulkDelete('Users', null, {});
}
};
种子脚本与路由无关,然后到运行脚本使用以下命令
npx sequelize-cli db:seed:all
此命令执行成功后,您的数据将存储在数据库中
在模型中我创建了一个文件
registerusers.js
const {Sequelize, Model,DataTypes}= require('sequelize')
const sequelize = new Sequelize("postgres","postgres","password",{
"host":"localhost",
"dialect":"postgres"
})
module.exports = {
regusers: async (email,password) =>{
console.log(email,password)
class RegisterUsers extends Model {
/**
* Helper method for defining associations.
* This method is not a part of Sequelize lifecycle.
* The `models/index` file will call this method automatically.
*/
static associate(models) {
// define association here
}
};
RegisterUsers.init({
email: DataTypes.STRING,
password: DataTypes.STRING,
//con_password: DataTypes.STRING
}, {
sequelize,
modelName: 'RegisterUsers',
schema:'shoppingcart'
});
// return RegisterUser;
await RegisterUsers.create({
email:`${email}`,
password:`${password}`,
// con_password:`${con_password}`
},
{
schema:'shoppingcart'
})
}
};
并将其添加到 router.js 文件
router.post('/register',async(req,res)=>{
//console.log(req.body);
const { email,password: plainTextPassword } = req.body
const password = await bcrypt.hash(plainTextPassword,10);
try{
// // console.log(email)
// // console.log(password)
let reguser = {
email: email,
password: password
}
let data = await models.regusers(reguser.email,reguser.password);
console.log(data)
}catch(error) {
console.log(error)
}
})
这从前端购买了表单数据到 postgres 数据库
必须post数据从FORMhtml到POSTGREStable通过migration sequelize orm 我通过迁移创建了 table....但无法将表单数据插入 table form.html
<form action="http://localhost:3000/register">
<fieldset class="p-4">
<input type="email" placeholder="Email*" id = "email" class="border p-3 w-100 my-2">
<input type="password" placeholder="Password*" id = "password" class="border p-3 w-100 my-2">
<input type="password" placeholder="Confirm Password*" id = "con_password" class="border p-3 w-100 my-2">
<div class="loggedin-forgot d-inline-flex my-3">
<input type="checkbox" id="registering" class="mt-1">
<label for="registering" class="px-2">By registering, you accept our <a class="text-primary font-weight-bold" href="terms-condition.html">Terms & Conditions</a></label>
</div>
<button type="submit" class="d-block py-3 px-4 bg-primary text-white border-0 rounded font-weight-bold" onclick="registrationform()">Register Now</button>
</fieldset>
</form>
下面是我的种子文件代码:
seeds/20210823074536-Users.js
module.exports = {
up: async (queryInterface, Sequelize) => {
router.post('/register',async(req,res)=>{
const { email,password: plainTextPassword } = req.body
const password = await bcrypt.hash(plainTextPassword,10);
try{
await queryInterface.bulkInsert('RegisterUsers', [{
email: email,
password: password ,
con_password: password
}])
}
catch(error) {
console.log(error)
}
});
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('RegisterUsers', null, {});
迁移/ 20210818092900-创建-注册-user.js
'use strict';
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.createTable('RegisterUsers', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
email: {
type: Sequelize.STRING
},
password: {
type: Sequelize.STRING
},
con_password: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: async (queryInterface, Sequelize) => {
await queryInterface.dropTable('RegisterUsers');
}
};
npx sequelize-cli seed:generate --name demo-user
此命令将在 seeders
文件夹中创建种子文件,类似于
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.bulkInsert('Users', [{
firstName: 'John',
lastName: 'Doe',
email: 'example@example.com',
createdAt: new Date(),
updatedAt: new Date()
}]);
},
down: (queryInterface, Sequelize) => {
return queryInterface.bulkDelete('Users', null, {});
}
};
种子脚本与路由无关,然后到运行脚本使用以下命令
npx sequelize-cli db:seed:all
此命令执行成功后,您的数据将存储在数据库中
在模型中我创建了一个文件 registerusers.js
const {Sequelize, Model,DataTypes}= require('sequelize')
const sequelize = new Sequelize("postgres","postgres","password",{
"host":"localhost",
"dialect":"postgres"
})
module.exports = {
regusers: async (email,password) =>{
console.log(email,password)
class RegisterUsers extends Model {
/**
* Helper method for defining associations.
* This method is not a part of Sequelize lifecycle.
* The `models/index` file will call this method automatically.
*/
static associate(models) {
// define association here
}
};
RegisterUsers.init({
email: DataTypes.STRING,
password: DataTypes.STRING,
//con_password: DataTypes.STRING
}, {
sequelize,
modelName: 'RegisterUsers',
schema:'shoppingcart'
});
// return RegisterUser;
await RegisterUsers.create({
email:`${email}`,
password:`${password}`,
// con_password:`${con_password}`
},
{
schema:'shoppingcart'
})
}
};
并将其添加到 router.js 文件
router.post('/register',async(req,res)=>{
//console.log(req.body);
const { email,password: plainTextPassword } = req.body
const password = await bcrypt.hash(plainTextPassword,10);
try{
// // console.log(email)
// // console.log(password)
let reguser = {
email: email,
password: password
}
let data = await models.regusers(reguser.email,reguser.password);
console.log(data)
}catch(error) {
console.log(error)
}
})
这从前端购买了表单数据到 postgres 数据库