OpenRecord: TypeError: User.create is not a function

Viewed 2701

ORM OpenRecord

I'm getting an error ( TypeError: User.create is not a function ) when I do the following:

await User.create(req.body.data)

When I log User I get this: [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:

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

openrecord github link

openrecord website documentation

2 Answers

Two small typos are validatePresenceOf (should be validatesPresenceOf) and validateFormatOf (should be validatesFormatOf).

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 e.g. express I recommend to start listening after the store has loaded:

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

Related