Parse-server: LiveQuery subscription.on Uncaught TypeError: subscription.on is not a function

Created on 14 Apr 2019  路  12Comments  路  Source: parse-community/parse-server

Issue Description

I'm getting Uncaught TypeError subscriptionUser.on is not a function while trying live queries with javascript

Steps to reproduce

let queryUser = new Parse.Query(userObj);
var subscriptionUser = queryUser.subscribe();

subscriptionUser.on('create', (object) => {
  totalNumberUsers = totalNumberUsers + 1;
  console.log('new user');
  document.getElementById("usersNum").innerHTML = totalNumberUsers;
});

Expected Results

No error.

Actual Outcome

subscriptionUser.on is not a function

Most helpful comment

await query.subscribe();

Or

query.subscribe().then(subscription => ...)

All 12 comments

query.subscribe returns a promise

Thanks for reporting. This is a breaking change and should be documented

query.subscribe returns a promise

Can you please give me a code example?

await query.subscribe();

Or

query.subscribe().then(subscription => ...)

Thank you, it worked !

No problem!

I'm sorry, but I'm a noob on Promises and I can't see how to go from the old to the new. I've got a script file that I listen to creates via livequery and I've tried await in an async function and I still get ...
"/home/travis/test/callbacks.js:98
subscriptionText.on('create', (newText) => {
^

TypeError: subscriptionText.on is not a function
at Object. (/home/travis/test/callbacks.js:98:21)
at Module._compile (module.js:653:30)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
at Function.Module.runMain (module.js:694:10)
at startup (bootstrap_node.js:204:16)
at bootstrap_node.js:625:3
"

I'm not sure how to use the "on" function/event properly with the new promise structure.

Thanks!

when I think it runs without issue, I get the following from my parse server.

"error: Connect message error %s". This definitely comes from when it runs the on("create"... function.

query.subscribe().then((subscriptionText) => {
.....
});

Thanks. I get no error from running the promise code, but I still get that second error on the Parse server side when trying to subscribe. Must be something out of sync between the two. hmmm.

Typings at @types/parse (v2.2.5) is still not updated. query.subscribe() still returns Parse.LiveQuerySubscription and triggers subscription.on is not a function error. I believe the return value should be Promise<Parse.LiveQuerySubscription>.

A temporary workaround is to modify the typings in the node_modules folder itself:

File: node_modules/@types/parse/index.d.ts on line 518

Change from:

...
subscribe(): LiveQuerySubscription;
...

to:

...
subscribe(): Promise<LiveQuerySubscription>;
...

NOTE:

This will cause inconsistencies especially in CI environments because you are modifying the file in node_modules folder directly.

I think my issue ended up being a change I had made when adding in push notifications. I had set the javascript key to null and I guess it needed to be double quotes for empty string. Odd, but that was causing my Connect message issue. I got the code working that @dplewis provided as long as I was on a the newer Parse server and the 2.4 parse in node for the SDK.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jaydeep82 picture jaydeep82  路  4Comments

ugo-geronimo picture ugo-geronimo  路  3Comments

omyen picture omyen  路  3Comments

dpaid picture dpaid  路  3Comments

kilabyte picture kilabyte  路  4Comments