Home > other >  Getting: typeError: .create is not a function whenever I try to pass data to database
Getting: typeError: .create is not a function whenever I try to pass data to database

Time:01-22

EDIT: Thanks. I wasn't linking to the right "model" and didn't realize because the filenames are basically the same. Hahaha.

Okay, so I know this is reaching the server; I can print any of the user inputted values in the console. However when I try to pass them to the database table, I get the error mentioned in the title.

**Actual model (oops):

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;

};

Script on frontend:

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);
        });
      }
    }
}

File containing the endpoint:

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."})
  });
});
}

Where's the issue here? I thought I was passing the row values defined in frontend to a blank row on backend.

CodePudding user response:

You are missing something.

What you posted as model is only a constructor of a class . There is no code that does something.

You need something like this.


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
});

Look here

Then when you start your app. You need to connect sequelize to your Database.

const sequelize = new Sequelize(connectionOptions)

CodePudding user response:

As Ralle Mc Black mentioned

What you posted as model is only a constructor of a class . There is no code that does something.

You need to use the model that you imported:

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."})
  });
});
}
  • Related