gpt4 book ai didi

mysql - 带有 Sequelize 的 Node js。无法插入表格

转载 作者:行者123 更新时间:2023-12-03 22:34:09 25 4
gpt4 key购买 nike

  • 背景
  • 使用 nodejs 版本 12。
  • 在 MySQL 数据库 dapdb 中创建了“user”表。
  • 当我尝试将记录写入表时 - authController - Postman 描述:
    “{
    "status": "成功💥",
    “数据”: {}
    }"
  • 没有数据写入表,这是问题。文件如下。

  • 服务器.js
        const dotenv = require('dotenv');
    dotenv.config({ path: './config.env' });
    const db = require('./database/db');
    const app = require('./app');
    ///////////////////////////////
    // 4. Start Server
    const port = process.env.PORT || 90;

    db.sequelize.sync().then(() => {
    app.listen(port, () => {
    console.log(`App running on port ${port}...`);
    });
    });
       // db.js
    const Sequelize = require('sequelize');
    const mysql = require('mysql');
    const db = {};
    const sequelize = new Sequelize(process.env.db, process.env.logonid, process.env.password, {
    host: process.env.host,
    port: process.env.dbport,
    dialect: 'mysql',
    });
    db.sequelize = sequelize;
    db.Sequelize = Sequelize;
    module.exports = db;
        //userModel.js
    const db = require('./../database/db');
    Sequelize = require('sequelize');
    const validate = require('validator');

    const User = db.sequelize.define('user', {
    name: {
    type: Sequelize.STRING,
    trim: true,
    required: [true, 'Please give me your name!'],
    len: [3 - 50],
    },
    email: {
    type: Sequelize.STRING,
    unique: true,
    trim: true,
    required: true,
    lowercase: true,
    validate: [validate.isEmail, 'Please provide a valid email.'],
    photo: Sequelize.STRING,
    password: {
    type: Sequelize.STRING,
    required: [true, 'Please provide a password.'],
    minlength: 8,
    },
    },
    password: {
    type: Sequelize.STRING,
    allowNull: false,
    },
    passwordConfirm: {
    type: Sequelize.STRING,
    required: [true, 'Please provide a password confirmation.'],
    },
    });
    module.exports = User;
        // authController.js
    const db = require('./../database/db');
    const User = require('./../models/userModel');
    exports.signup = async function (req, res, next) {
    const newUser = await db.sequelize.sync().then(function () {
    User.create({
    name: req.body.name,
    email: req.body.email,
    password: req.body.password,
    passwordConfirm: req.body.passwordConfirm,
    });
    console.log(req.body);
    });
    res.status(201).json({
    status: `Success 💥`,
    data: {
    user: newUser,
    },
    });
    };
          // app.js
    const express = require('express');
    const morgan = require('morgan');

    const userRouter = require('./routes/userRoutes');
    const app = express();
    ///////////////////////////////
    // 1. Middlewares

    if (process.env.NODE_ENV === 'development') {
    app.use(morgan('dev'));
    }
    app.use(express.json());
    app.use(express.static(`${__dirname}/public`));
    app.use((req, res, next) => {
    // console.log('Hello from the middleware');
    next();
    });
    app.use((req, res, next) => {
    req.requestTime = new Date().toISOString();
    next();
    });
    app.use('/api/v1/users', userRouter);
    module.exports = app;

    最佳答案

    你的结果没有返回任何东西。你可以试试这个:

     const db = require('./../database/db');
    const User = require('./../models/userModel');
    exports.signup = async function (req, res, next) {
    const newUser = await User.create({
    name: req.body.name,
    email: req.body.email,
    password: req.body.password,
    passwordConfirm: req.body.passwordConfirm,
    });
    console.log(req.body);
    res.status(201).json({
    status: `Success 💥`,
    data: {
    user: newUser,
    },
    });
    };

    关于mysql - 带有 Sequelize 的 Node js。无法插入表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63200838/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com