NodeJS 正在获取:类型错误:每当我尝试将数据传递到数据库时,.create不是函数

nfs0ujit  于 2023-01-25  发布在  Node.js
关注(0)|答案(2)|浏览(276)

编辑:谢谢。我没有链接到正确的"模型",也没有意识到,因为文件名基本上是一样的。哈哈哈。
好了,我知道这是到达服务器;我可以在控制台中打印任何用户输入的值。但是当我试图将它们传递到数据库表时,我得到了标题中提到的错误。

    • 实际型号(哎呀):
module.exports = (sequelize, Sequelize) => {
const KitchenAssignment = sequelize.define("kitchenAssignments", 
{
  kAName: {
    type: Sequelize.STRING  
  },
  startTime: {
    type: Sequelize.TIME
  },
  endTime: {
    type: Sequelize.TIME
  },
  minRoleRequired: {
    type: Sequelize.ENUM("Leader", "Aid", "Cook", "Junior", "Dishwasher")
  }
});
return KitchenAssignment;

};
前端脚本:

export default {
    name: 'sendAssignment',
    data(){
        return {
          kAName: '',
          startTime: '',
          endTime: '',
          minRoleRequired:''
        };
    },
    methods: {
      sendInput(){
        let kitchenAssignment = {
          kAName: this.kAName,
          startTime: this.startTime,
          endTime: this.endTime,
          minRoleRequired: this.minRoleRequired
        }
        axios.post('/api/auth/kitchenAssignments', kitchenAssignment)
        .then(response => {
          console.log(response.data);
        })
        .catch(error => {
          console.log(error);
        });
      }
    }
}

包含终结点的文件:

const bodyParser = require ('body-parser');
//const { default: KitchenAssignment } = require('../../frontend/src/models/kitchenAssignment');
//const kitchenAssignment = require('../models/kitchenAssignment.model');
const db = require("../models");
const kitchenAssignment = db.KitchenAssignment; 

module.exports = function(app) {
app.use(bodyParser.json()); 
app.post('/api/auth/kitchenAssignments', (req, res) => {
  KitchenAssignment.create({
    kAName: req.body.kAName, 
    startTime: req.body.startTime, 
    endTime: req.body.endTime, 
    minRoleRequired: req.body.minRoleRequired
  })
  .then(KitchenAssignment => res.json(KitchenAssignment))
  .catch(error => {
    console.log(error);
    res.status(500).json({ message: "ERROR STORING INPUT."})
  });
});
}

问题出在哪里?我以为我把frontend中定义的行值传递给了backend中的一个空行。

ohtdti5x

ohtdti5x1#

你错过了什么。
你发布的模型只是一个类的构造函数,没有做任何事情的代码。
你需要这样的东西。

const { Sequelize, DataTypes } = require('sequelize');
const sequelize = new Sequelize('sqlite::memory:');

const User = sequelize.define('User', {
  // Model attributes are defined here
  firstName: {
    type: DataTypes.STRING,
    allowNull: false
  },
  lastName: {
    type: DataTypes.STRING
    // allowNull defaults to true
  }
}, {
  // Other model options go here
});

看啊
然后当你启动你的应用程序。你需要连接到你的数据库sequelize。

const sequelize = new Sequelize(connectionOptions)
dsf9zpds

dsf9zpds2#

就像Ralle Mc Black提到的
你发布的模型只是一个类的构造函数,没有做任何事情的代码。
您需要使用导入的模型:

app.post('/api/auth/kitchenAssignments', (req, res) => {
  kitchenAssignment.create({ // <-- Lowercase "k"
    kAName: req.body.kAName, 
    startTime: req.body.startTime, 
    endTime: req.body.endTime, 
    minRoleRequired: req.body.minRoleRequired
  })
  .then(kitchenAssignment => res.json(KitchenAssignment)) // <-- Also, lowercase K
  .catch(error => {
    console.log(error);
    res.status(500).json({ message: "ERROR STORING INPUT."})
  });
});
}

相关问题