Problem with observe method

Hi Fusetools team.

The observe method of Observable is not working for me at the moment. I tried to copy and paste the example you have on docs but still saying the same error.

I’m using Fuse 0.28.0 (build 8142) in OSX 10.11.4 at Local Preview and Android targets.

MainView.js:

var Observable = require("FuseJS/Observable");

var xVals = Observable(1, 20, 14);
var a = Observable(2);
var b = Observable(3);
var yVals = xVals.observe(a, b).map(function(x) {
    return a.value*x+b.value;
});

MainView.ux:

<App>
<JavaScript File="MainView.js"/>
</App>

test.unoproj:

{
  "RootNamespace":"",
  "Packages": [
  	"Fuse",
  	"FuseJS"
  ],
  "Includes": [
    "*"
  ]
}

Error log:

LOG: Error: JavaScript error in MainView.js line 6: Name: TypeError: xVals.observe is not a function
    Error message: Uncaught TypeError: xVals.observe is not a function
    File name: MainView.js
    Line number: 6
    Source line: var yVals = xVals.observe(a, b).map(function(x) {
    JS stack trace: TypeError: xVals.observe is not a function
        at null._tempMethod (MainView.js:6:19)
     in Fuse.Reactive.JavaScript</usr/local/share/uno/Packages/Fuse.Reactive/0.39.2/$.uno:1444>

Thank you.

Hi there, unfortunately observe has not actually been released; we made a small mistake in our documentation and released docs for that function too soon. The docs have been updated removing this function. Sorry about that!

If you provide a little more info about your use case specifically, I can probably help you find an alternative :slight_smile:

Hi Jake,

I understand. Don’t worry about it.

I’m developing an application that uses meteor DPP protocol. When I subscribe to a meteor collection, the data arrives record by record in an async fashion.

One of the collections I have to subscribe to is “messages” which has a relationship with “users” collection. But, in order to display the information correctly I have to join reactively that two collections in the client.

One of my firsts attempts to do that was something like:

var messages = messages_src.map(function(mitem) {
  users.forEach(function(uitem) {
    if(mitem.sender_id == uitem.id) {
      mitem.name = uitem.profile.first_name + " " + uitem.profile.last_name;
      return mitem;
    }
  });
  return mitem;
});

But unfortunately, that code doesn’t react to changes in “users” collection.

Then was when I thought about using observe method in order to react to changes in “users” collection.

At the end, I could resolve that case with the following code:

var messages = users.map(function(u) {
    return messages_src.where({id_sender: u.id}).map(function(um) {
      um.name = u.profile.first_name + " " + u.profile.last_name;
      return um;
    });
  });

But if you have a better or more efficient way to accomplish that, you’re welcome.

Very thanks Jake.

The second piece of code in your previous post should work, yeah. I’m not aware off the top of my head of a faster solution when you have to react to/join two separate collections asynchronously. It’s unfortunate that you have to join the two tables in your client; this is the kind of thing databases/backends should be particularly good at :slight_smile:

However, since you’re getting both collections updated asynchronously, does the order of the items matter? I suppose it would in the case of messages + users, but if that’s not the case, you might be able to find a solution using flatMap and where. The docs on where outline how this can be done: https://www.fusetools.com/docs/fusejs/observable#where-condition