Hi, I'm just getting my feet wet with the framework and was able to successfully save a User lucid model to the database (using mysql2), but for some reason, a second model that I created is throwing an error whenever I try to save it. The stack frame is showing the following line in the Lucid source code as the source of the error: https://github.com/adonisjs/adonis-lucid/blob/develop/src/Lucid/Model/index.js#L583.
So in Lucid's _insert() method, it seems likethis.constructor.$hooks is undefined and that seems to be creating the error. I'm not doing anything whatsoever with adding my own hooks, so I'm not sure why this is happening. Below is the model that I've created (after generating it with adonis make:model Device), and the error is occurring at line 20, in the register function, when I perform await this.save():
'use strict'
const uuid = require('uuid');
const Model = use('Model');
class Device extends Model {
static boot () {
super.boot()
}
async register(userId, pushToken, deviceId, deviceName, deviceType) {
this.user_id = userId;
this.push_token = pushToken;
this.device_id = deviceId;
this.device_name = deviceName;
this.device_type = deviceType;
this.api_token = this.generateApiToken();
await this.save();
}
generateApiToken() {
return uuid.v4();
}
}
module.exports = Device
And here is the migration for the model (which I've already run), if that helps:
'use strict'
const Schema = use('Schema')
class DevicesSchema extends Schema {
up () {
this.create('devices', (table) => {
table.increments()
table.integer('user_id').unsigned().references('id').inTable('users').notNull().onDelete('cascade');
table.string('push_token', 255).notNull();
table.string('device_id', 255).notNull();
table.string('device_name', 255).notNull();
table.string('device_type', 255).notNull();
table.string('api_token', 255).notNull();
table.timestamps()
})
}
down () {
this.drop('devices')
}
}
module.exports = DevicesSchema
Really appreciate the help and all of the work that's been done on the framework! Thank you!
Hey @mattmallon! 馃憢
Could you please also give us the code you used to save your model?
Note that you don't need to have the boot method since it's only calling the parent boot method.
Hi @RomainLanz , thank you for the quick reply! Here's the controller I'm calling from, the function is being called at line 17, await device.register(user.id, pushToken, deviceId, deviceName, deviceType);:
'use strict'
const Device = require('../../Models/Device');
const User = require('../../Models/User');
class DeviceController {
async create({ request, response }) {
const params = request.all();
const username = params.username;
let user = await User.query().where('username', username).firstOrFail();
let pushToken = params.pushToken;
let deviceId = params.deviceId;
let deviceName = params.deviceName;
let deviceType = params.deviceType;
let device = new Device();
await device.register(user.id, pushToken, deviceId, deviceName, deviceType);
return response.send(device);
}
}
module.exports = DeviceController
Do you still have the issue if you put all the code that you have in Device.register() into your controller?
Other note, you can use the use method instead of require to avoid having to use the relative path.
const Device = require('../../Models/Device');
const User = require('../../Models/User');
// to
const Device = use('App/Models/Device');
const User = use('App/Models/User');
Thank you @RomainLanz , it was the lack of use() statements that apparently was causing the problem! Sorry, force of habit with require() and I didn't realize that use() was so vital, since I was able to save my first model without any issues. I will make sure to go all in with use() going forward! Appreciate your help.
Most helpful comment
Do you still have the issue if you put all the code that you have in
Device.register()into your controller?Other note, you can use the
usemethod instead of require to avoid having to use the relative path.