0

我知道关于 sequelize 的材料很多,但我的问题非常具体,我似乎无法在任何地方找到答案。我正在尝试使用 mvc 设置将我的全栈应用程序的用户输入保存到我的数据库中

const restaurants = require('../models').Restaurants;

module.exports ={
  create(req, res){
    return
    Restaurants.create({
      name: req.body.name,
      foodType: req.body.foodType,
      price: req.body.price,
      location: req.body.location
    })

    .then(console.log(req.body))
    .catch(error, function(){
      res.send(error);
    });
  }
}

上面的代码没有产生任何错误,但是当我使用本地服务器访问我的路线并提交输入时,它不会将任何数据保存到我的数据库中。我看过很多关于 .sync 和 sequelize 实例的教程,但是当我包含它时会引发错误,我认为这是因为,当我使用 sequelize init 时,它会自动在我的模型文件夹中创建一个名为 index 的文件.js,我相信其中已经包含了该代码。

这是我的模型文件包含的内容

'use strict';
module.exports = (sequelize, DataTypes) => {
  var Restaurants = sequelize.define('Restaurants', {
    name: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    foodtype: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    price: {
      type: DataTypes.INTEGER,
      allowNull: false,
    },
    location: {
      type: DataTypes.STRING,
      allowNull: false,
    },
  }, {});
  Restaurants.associate = function(models) {
    // associations can be defined here
  };
  return Restaurants;
};

请让我知道我能做什么。非常感谢,提前!

4

0 回答 0