stimulusreflex / stimulus_reflex

Build reactive applications with the Rails tooling you already know and love.
https://docs.stimulusreflex.com
MIT License
2.28k stars 172 forks source link

Cannot read property 'schema' of undefined #282

Closed scottbarrow closed 4 years ago

scottbarrow commented 4 years ago

Bug Report

Describe the bug

A clear and concise description of what the bug is.

To Reproduce

Followed the get started guide Rails new

Expected behavior

reflex to trigger method

Screenshots or reproduction

stimulus_reflex.js:237 Uncaught TypeError: Cannot read property 'schema' of undefined at stimulus_reflex.js:237 at utils.js:30

Versions

StimulusReflex

Rails 6.0.3

External tools

Browser

scottbarrow commented 4 years ago

it seems the generator stimulus_reflex:install is missing these lines from app/javascript/controllers/index.js

import StimulusReflex from "stimulus_reflex";
import consumer from "../channels/consumer";
StimulusReflex.initialize(application, { consumer });
scottbarrow commented 4 years ago

also missing is the line passing the controller to StimulusReflex allowing it to use the Stimulus Controllers. I don't see this documented I presume this is the desired setup?

import controller from "./application_controller";`
StimulusReflex.initialize(application, { consumer, controller });
leastbad commented 4 years ago

Hey Scott,

When you say that it's missing, do you mean that it's not adding them, or adding them again?

leastbad commented 4 years ago

Also, I'm pretty sure that you're not supposed to be passing the application_controller to the initialize method that way. That looks like the precise wrong thing, actually. I no longer remember why that option exists - there was a time when passing a controller into the method made more sense than it does now, but I'm pretty sure (close to 100% sure) it's not supposed to be receiving the application_controller generated by the installing.

I'm personally not a huge fan of the application_controller pattern because in practice it generates a lot of confusion. You might simplify your scenario by setting up a controller like this:

import { Controller } from 'stimulus'
import StimulusReflex from 'stimulus_reflex'

export default class extends Controller {
  connect () {
    StimulusReflex.register(this)
  }

  afterReflex () {
    console.log('ping')
  }

even if it's just to see what's up. You don't have to use application_reflex.

scottbarrow commented 4 years ago

Hey Scott,

When you say that it's missing, do you mean that it's not adding them, or adding them again?

yeah its not adding them, but the https://docs.stimulusreflex.com/setup#manual-configuration suggests they should be

scottbarrow commented 4 years ago

If I don't pass the controller into the StimulusReflex.initialize call my reflex works, but none of the lifecycle methods get called in the js controllers. see https://github.com/hopsoft/stimulus_reflex/issues/283

leastbad commented 4 years ago

Hi Scott. I saw that you closed the issue. Did you get everything sorted out?

I actually wrote the documentation, for better or for worse, as well as having contributed to the install process. I believe you when you say that it's not adding the correct lines to your index.js, although most don't have a problem with it.

If you fire up a vanilla Rails app (rails new) and run the SR install rake task as directed, does it work for you?

I'm glad that you're no longer passing the application_controller into the SR initializer... definitely not what you're supposed to do.

As for the lifecycle callbacks... if you create the vanilla app as I suggested a few moments ago, if you put some callbacks in your controller, do they work?

Post that repo to Github and share the url and I'll clone it if you're still having troubles.

scottbarrow commented 4 years ago

@leastbad I did thanks I think the only parts missing from the generator install were below, which are detailed in the manual installed method

import StimulusReflex from "stimulus_reflex";
import consumer from "../channels/consumer";
StimulusReflex.initialize(application, { consumer });

The lifecycle callbacks are now working, I was not calling super.method() in my child controllers

leastbad commented 4 years ago

I'm glad that you got sorted!

Super weird that the configuration isn't working as part of the install. I'll look into it.