OpenRecord:TypeError:User.create不是一个函数

时间:2018-11-15 14:20:33

标签: javascript node.js openrecord.js

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 个答案:

答案 0 :(得分:1)

两个小错别字是validatePresenceOf(应该验证 s PresenceOf)和validateFormatOf(应该验证 s FormatOf)。

但是,这不是您遇到的错误! 我敢肯定,您忘了等到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()