0

続編に関する資料がたくさんあることは知っていますが、私の質問は非常に具体的で、どこにも答えが見つからないようです. 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