类型错误:Job.create 不是函数

问题描述 投票:0回答:1

我在项目中使用sequelize,并从模型文件夹导入Job(连接到数据库的变量),但是当我使用Job.create时,在将数据插入postman时会返回此错误: 类型错误:Job.create 不是函数

我检查了与数据库的连接,它可以工作,但是 Job.create 不起作用。 代码:

**//in models at Jobs.js file**
const Sequelize = require('sequelize');
const db = require('../db/connection');
//Define
const Job = db.define('job', {
    title:{
        type: Sequelize.STRING,

    },
    description:{
        type: Sequelize.STRING
    },
    salary:{
        type: Sequelize.STRING
    },
    company:{
        type: Sequelize.STRING
    },
    email:{
        type: Sequelize.STRING
    },
    new_job:{
        type: Sequelize.INTEGER
    }
});
**// in routes at job.js file**
const express = require('express');
const router  = express.Router();
const Job = require('../models/Job')
//test
router.get('/test',(req, res)=>{
    res.send("As rotas estão funcionando");
})
//add
router.post("/add", (req, res)=>{
    let {title, salary, company, description, email, new_job} = req.body;
    //insert 
    Job.create({
        title,
        salary,
        company,
        description,
        email,
        new_job
    })
    .then(()=>{
        res.redirect('/');
    })
    .catch(err =>{
        console.log(err);
    });
});

module.exports = router;

database postman sequelize.js router require
1个回答
0
投票

我解决了,我忘了给 module.exports = Job ;-;

© www.soinside.com 2019 - 2024. All rights reserved.