I have Observable issues

I am trying to generate random numbers and then it works. Based on my code I also want to randomly select one out of the 5 random numbers as a correct answer. but when I do that and I want to see the value of the correct answer I dont see anything. Can someone tell me what I am doing wrong and how to go about it. Here is my Js code:

var Observable = require("FuseJS/Observable");
// var random1 = Observable();
var selector = Observable();
var random1 =Observable();
var random2 =Observable();
var random3 =Observable();
var random4 =Observable();
var random5 =Observable();
var correct_answer = Observable();

function gen() {
    // body...
    random1.value = Math.floor(Math.random()*(21-1) + 1);
    random2.value = Math.floor(Math.random()*(31-11) + 11);
    random3.value = Math.floor(Math.random()*(41-21) + 21);
    random4.value = Math.floor(Math.random()*(51-31) + 31);
    random5.value = Math.floor(Math.random()*(71-41) + 41);
    selector.value = Math.floor(Math.random()*(5-0) + 0);
    correct_answer.value = Observable(random1,random2,random3,random4,random5);
}

function generate() {
    // body...
    gen();
    correct_answer.value = correct_answer.getAt(selector);
    correct_answer.value = correct_answer;
}
// generate();


module.exports = {
    random1: random1,
    random2: random2,
    random3: random3,
    random4: random4,
    random5: random5,
    correct_answer: correct_answer,
    generate: generate,
}

Hey,

I’m not sure if this fixes your problem but correct_answer.value = Observable(random1,random2,random3,random4,random5); should be

correct_answer.add(random1,random2,random3,random4,random5);

Fudge I get a

Not Handled: OnAdd error when I use the add function

Try this:

correct_answer.add(random1);
correct_answer.add(random2);
correct_answer.add(random3);
correct_answer.add(random4);
correct_answer.add(random5);

I’ve had a problem with adding multiple values before

I just tried it and my UI went white… lol

the app crashes


        <!-- page for the game -->
        <gamePage ux:Name="gameBoard" Background="#000">
            <!-- <txt_demoHeader Value="Start games" TextColor="#FFFFFF"/> -->
            <StackPanel>
                <Panel>
                    <txt_menu Value="500pts" TextColor="#FFFFFF" Alignment="Left" Margin="20"/>
                    <txt_menu Value="X" TextColor="#FFFFFF" Alignment="Right" Margin="20">
                        <Clicked>
                            <NavigateTo Target="mainMenu" NavigationContext="appNav"/>
                        </Clicked>
                    </txt_menu>
                </Panel>
                <txt_question Value="TumboTubmo, What is on my mind?" TextColor="#FFFF00" TextWrapping="Wrap"/>
                <txt_question Value="{correct_answer}" TextColor="#FFFF00" TextWrapping="Wrap"/>
                <Panel Alignment="Top" Padding="0,-100,0,0">
                    <fc1>
                        <Stroke Brush="#00FFFF" Width="6"/>
                        <txt_options Value="{random5}"/>
                        <Clicked>
                            <Scale Easing="BackIn" Factor="1.3" Duration="0.2"/>
                            <!-- <Vibrate Duration="0.2" /> -->
                        </Clicked>
                    </fc1>
                    <fc2 Alignment="Left" Margin="60,50,20,5">
                        <Stroke Brush="#CCFF66" Width="6"/>
                        <txt_options Value="{random4}"/>
                        <Clicked>
                            <Scale Easing="BackIn" Factor="1.3" Duration="0.2"/>
                            <!-- <Vibrate Duration="0.2" /> -->
                        </Clicked>
                    </fc2>
                    <fc3 Alignment="Center" Margin="-40,230,20,5">
                        <Stroke Brush="#FF0000" Width="6"/>
                        <txt_options Value="{random3}"/>
                        <Clicked>
                            <Scale Easing="BackIn" Factor="1.3" Duration="0.2"/>
                            <!-- <Vibrate Duration="0.2" /> -->
                        </Clicked>
                    </fc3>

                    <fc1 Alignment="Right" Margin="0,150,50,25">
                        <Stroke Brush="#0000FF" Width="6"/>
                        <txt_options Value="{random2}"/>
                        <Clicked>
                            <Scale Easing="BackIn" Factor="1.3" Duration="0.2"/>
                            <!-- <Vibrate Duration="0.2" /> -->
                        </Clicked>
                    </fc1>

                    <fc2 Alignment="Right" Margin="0,300,80,5" Clicked="{generate}">
                        <Stroke Brush="#FF3CFC" Width="6"/>
                        <txt_options Value="c"/>
                        <!-- <Clicked>
                            <Scale Easing="BackIn" Factor="1.3" Duration="0.2"/>
                            <Vibrate Duration="0.2" />
                        </Clicked> -->
                    </fc2>
                </Panel>
            </StackPanel>
        </gamePage>

Preview detected an unhandled exception: Uno.AggregateException: One or more errors occurred. at Fuse.UpdateManager.Update(Fuse.Stage) at Fuse.UpdateManager.Update() at Fuse.AppBase.OnUpdate() at Outracks.Simulator.Application.OnUpdate() at Fuse.App.FrameCallback.doFrame(long) ERROR: Not handled: OnAdd Uno.Exception occured. at Fuse.Reactive.DataBinding1.Fuse.Reactive.IObserver.OnAdd(object) at Uno.Threading.IDispatcherExtensions.Arg1Invoke1.Run() at Fuse.Reactive.Dispatcher.Run() at Fuse.UpdateListener.Invoke() at Fuse.UpdateManager.Update(Fuse.Stage) at Fuse.UpdateManager.Update() at Fuse.AppBase.OnUpdate() at Outracks.Simulator.Application.OnUpdate() at Fuse.App.FrameCallback.doFrame(long)

Hi!

I think the problem is here:

correct_answer.value = Observable(random1,random2,random3,random4,random5);

Why is the correct answer a list of values?

The correct answer is gotten when I use the getAt()

function gen() {
    // body...
    random1.value = Math.floor(Math.random()*(21-1) + 1);
    random2.value = Math.floor(Math.random()*(31-11) + 11);
    random3.value = Math.floor(Math.random()*(41-21) + 21);
    random4.value = Math.floor(Math.random()*(51-31) + 31);
    random5.value = Math.floor(Math.random()*(71-41) + 41);
    selector.value = Math.floor(Math.random()*(5-0) + 0);
    correct_answer.value = Observable(random1,random2,random3,random4,random5);
}

function generate() {
    // body...
    gen();
    correct_answer.value = correct_answer.getAt(selector);
    correct_answer.value = correct_answer;
}

An observable must either be a single value, or a list of values. If you databind it to a property, it must be a single value. Only if you use it for a list property such as <Each Items="{..}"> can it be an array.

A problem in your code is that correct_answer switches between being a list and a single value.

I have decided to make the correct_answer a non list variable and I have created another list variable:

var group_of_answer = Observable();
var correct_answer;


function generate() {
    // body...
    random1.value = Math.floor(Math.random()*(21-1) + 1);
    random2.value = Math.floor(Math.random()*(31-11) + 11);
    random3.value = Math.floor(Math.random()*(41-21) + 21);
    random4.value = Math.floor(Math.random()*(51-31) + 31);
    random5.value = Math.floor(Math.random()*(71-41) + 41);
    selector.value = Math.floor(Math.random()*(5-0) + 0);
    group_of_answer.add(random1,random2,random3,random4,random5);
    correct_answer.value = group_of_answer.getAt(selector);
}

But I get an error

Name: TypeError: Cannot set property ‘value’ of undefined Error message: Uncaught TypeError: Cannot set property ‘value’ of undefined File name: /Users/chizom/Documents/AndroidProject/tumbotumbo/Js/Logic.js Line number: 22 Source line: correct_answer.value = group_of_answer.getAt(selector); JS stack trace: TypeError: Cannot set property ‘value’ of undefined at generate (/Users/chizom/Documents/AndroidProject/tumbotumbo/Js/Logic.js:22:23)

var correct_answer; Should be var correct_answer = Observale();

Yes, I just changed it when I realised and I saw that when I clicked my button I got the message on my UI saying “Fuse.Reactive.Observable” instead of a single number

When I make the the Js code like this and I test


var Observable = require("FuseJS/Observable");
// var random1 = Observable();
var selector = Observable();
var random1 =Observable();
var random2 =Observable();
var random3 =Observable();
var random4 =Observable();
var random5 =Observable();
var group_of_answer = Observable();
var correct_answer = Observable();


function generate() {
    // body...
    random1.value = Math.floor(Math.random()*(21-1) + 1);
    random2.value = Math.floor(Math.random()*(31-11) + 11);
    random3.value = Math.floor(Math.random()*(41-21) + 21);
    random4.value = Math.floor(Math.random()*(51-31) + 31);
    random5.value = Math.floor(Math.random()*(71-41) + 41);
    selector.value = Math.floor(Math.random()*(5-0) + 0);
    group_of_answer.add(random1,random2,random3,random4,random5);
    group_of_answer.getAt(selector);
    correct_answer.value = group_of_answer; 
}
// generate();


module.exports = {
    random1: random1,
    random2: random2,
    random3: random3,
    random4: random4,
    random5: random5,
    correct_answer: group_of_answer,
    generate: generate,
}

I get this error

LOG:

Preview detected an unhandled exception: Uno.AggregateException: One or more errors occurred.
   at Fuse.UpdateManager.Update(Fuse.Stage)
   at Fuse.UpdateManager.Update()
   at Fuse.AppBase.OnUpdate()
   at Outracks.Simulator.Application.OnUpdate()
   at Fuse.App.FrameCallback.doFrame(long)

ERROR: Not handled: OnAdd Uno.Exception occured. at Fuse.Reactive.DataBinding1.Fuse.Reactive.IObserver.OnAdd(object) at Uno.Threading.IDispatcherExtensions.Arg1Invoke1.Run() at Fuse.Reactive.Dispatcher.Run() at Fuse.UpdateListener.Invoke() at Fuse.UpdateManager.Update(Fuse.Stage) at Fuse.UpdateManager.Update() at Fuse.AppBase.OnUpdate() at Outracks.Simulator.Application.OnUpdate() at Fuse.App.FrameCallback.doFrame(long)

That still looks like the same error, to me. Can’t help more without a complete test project (zip).

This needs to be in the docs somehow.
Also, if I have
sections = new Observable();

and an array like this:
var mySections = [1,2,3];

setting the Observable and trying to use <Each> on it doesn’t work:
sections.value = mySections;

Apparently, if you want to data-bind a list, it can’t be an Observable whose single value is a list.

Also, your error messages need to get a lot better.
If there’s a problem with the databinding (like the one above),
I need a better error message.

If a binding can’t be resolved, I need at least a warning.

Thanks,
Jochen

sections.refreshAll(mySections)