代码之家  ›  专栏  ›  技术社区  ›  kockburn

OpenRecord:TypeError:User.create不是函数

  •  0
  • kockburn  · 技术社区  · 7 年前

    ORM OpenRecord

    TypeError: User.create is not a function )当我执行以下操作时:

    await User.create(req.body.data)
    

    当我登录用户时,我得到以下信息: [Function: User]

    数据库配置:

    //config/database/openRecord.js
    /** more code above */
    let store = new Store({
        database, 
        user, 
        password, 
        host,
        autoConnect: true,
        autoAttributes: true,
        models: require("../../app/models/models")
    }); 
    

    //app/models/models.js
    module.exports = [
        require("./user")
    ]
    

    型号:

    //app/models/user.js
    const Store = require('openrecord/store/mysql');
    
    class User extends Store.BaseModel {
        static definition(){
            this.validatePresenceOf(
                'first_name', 'last_name', 'email', 'password'
            );
    
            this.validatesConfirmationOf('password');
            this.validateFormatOf('email', 'email');
        }
    
        fullName(){
            return `${this.first_name} ${this.last_name}`
        }
    }
    
    module.exports = User;
    

    当我以正确的方式清楚地定义了我的对象时,为什么会抛出错误?

    openrecord github link

    openrecord website documentation

    1 回复  |  直到 7 年前
        1
  •  1
  •   Philipp Waldmann    7 年前

    validatePresenceOf (应该是有效的 S validateFormatOf (应该是有效的 S 格式化)。

    然而,这不是你所经历的错误! the store is ready .

    如果你使用例如。 express 我建议在存储加载后开始侦听:

    const express = require('express')
    const store = require('./store')
    
    const app = express()
    
    async function start() {
      // add middleware ...
    
      await store.ready()
    
      // start express server
      app.listen()
    }
    start()
    
    推荐文章