简体   繁体   English

OpenRecord:TypeError:User.create 不是 function

[英]OpenRecord: TypeError: User.create is not a function

ORM OpenRecord ORM OpenRecord

I'm getting an error ( TypeError: User.create is not a function ) when I do the following:当我执行以下操作时,出现错误( TypeError: User.create is not a function ):

await User.create(req.body.data)

When I log User I get this: [Function: User]当我登录用户时,我得到这个: [Function: User]

Database config:数据库配置:

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

Models:楷模:

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

Model: Model:

//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;

Why is it throwing an error when I've clearly defined my object the correct way?当我以正确的方式明确定义我的 object 时,为什么会引发错误?

openrecord github link打开记录 github 链接

openrecord website documentation openrecord 网站文档

Two small typos are validatePresenceOf (should be validate s PresenceOf) and validateFormatOf (should be validate s FormatOf). 两个小错别字是validatePresenceOf (应为validate PresenceOf)和validateFormatOf (应为validate FormatOf)。

However, this is not the error you experience! 但是,这不是您遇到的错误! I'm pretty sure that you forget to wait until the store is ready . 我敢肯定你会忘记等到商店准备好为止。

If you use eg express I recommend to start listening after the store has loaded: 如果使用例如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()

You have not exported the module.您尚未导出模块。 for example we write module.exports=mongoose.model("user",userobj) this is missing例如我们写 module.exports=mongoose.model("user",userobj) 这是缺失的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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