简体   繁体   中英

How to change sequelize model.findOne() target table?

I am new to sequelize.js . I have one model User. I try to use findOne function to check whether the user is existed or not.

This is my user model.

const Sequelize = require('sequelize')
const db = require('../database/db.js')

module.exports = db.sequelize.define(
  'user',
  {
    id: {
      type: Sequelize.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    name: {
      type: Sequelize.STRING
    },
    email: {
      type: Sequelize.STRING
    },
    username: {
      type: Sequelize.STRING
    },
    password: {
      type: Sequelize.STRING
    },
    address: {
      type: Sequelize.STRING
    },
    created: {
      type: Sequelize.DATE,
      defaultValue: Sequelize.NOW
    },
    role: {
      type: Sequelize.STRING
    }
  },
  {
    timestamps: false
  }
)

This is how I check the user.

const express = require('express')
const users = express.Router()
const cors = require('cors')
const jwt = require('jsonwebtoken')
const bcrypt = require('bcrypt')

const User = require('../../models/User')
users.use(cors())

process.env.SECRET_KEY = 'secret'

users.post('/register', (req, res) => {
    console.log("testing register zzz");
  const today = new Date()
  const userData = {
    name: req.body.name,
    email: req.body.email,
    username: req.body.username,
    password: req.body.password,
    address: req.body.address,
    created: today,
    role: req.body.role
  }

  User.findOne({
    where: {
      email: req.body.email
    }
  }).then(user => {
      if (!user) {
          console.log("not inside database");
        bcrypt.hash(req.body.password, 10, (err, hash) => {
          userData.password = hash
          User.create(userData)
            .then(user => {
              res.json({ status: user.email + 'Registered! liao' })
            })
            .catch(err => {
              res.send('error: ' + err)
            })
        })
      } else {
          console.log("inside database");
        res.json({ error: 'User already exists' })
      }
    })
    .catch(err => {
        console.log("??????  "+err);
      res.send('error: ' + err)
    })
})

However, I received an error show me that table users does not exist. The table name in mysql database is user . How to change the findOne target table name so that findOne function search the user table instead of users table?

You can set freezeTableName attribute of a model to true to disable pluralization of model names that sequelize does by default.

Please refer to the following example given in sequelize docs

var User = sequelize.define('user', {
  firstName: {
    type: Sequelize.STRING,
    field: 'first_name' // Will result in an attribute that is firstName when user facing but first_name in the database
  },
  lastName: {
    type: Sequelize.STRING
  }
}, {
  freezeTableName: true // Model tableName will be the same as the model name
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM