Firebase Web 2.4.0承诺Nodejs [英] Firebase Web 2.4.0 Promises in Nodejs

查看:148
本文介绍了Firebase Web 2.4.0承诺Nodejs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在节点应用程序中遇到了一些来自Firebase Web的错误回调问题。




$ b $原来的问题是,从一个.set错误回调显然没有被激发,当我预期它。 b

在调试过程中,我将Firebase npm模块升级到最新的2.4.0(NodeJS 0.10.28),然后注意到异步调用有一个新的Promise语法。



我没有找到使用新语法的例子,但是做了一个小提琴
$ b

  var fb = new Firebase(https: //katowulf-examples.firebaseio.com/); 

//监视变化和更新UI
尝试{
fb.child('sorted_widgets')。orderByChild('name')。equalTo('one')。once 'value')。then(handleSuccess).catch(handleError)
fb.child(undefined).once('value')。then(handleSuccess).catch(handleError)
fb.child(' catch(err){
console.error('Caught a error',err)$ b(handleError)
$ b $ catch(err)

$ b函数handleSuccess(snap){
console.log('snap:',snap.val())
}

函数handleError(err){
console.log('error:',err)
}


$ b $我现在试图在我的Node应用程序中使用该语法,并得到这个错误:

$ p $ TypeError:Object#< ; NB>没有方法'catch'
at myFunction(/node_scripts/my_script.js:514:19)
at c(/node_modules/firebase/lib/firebase-node.js:238:58)
at /node_modules/firebase/lib/firebase-node.js:199:710
at ec(/node_modules/firebase/lib/firebase-node.js:52:165)
at ac( /node_modules/firebase/lib/firebase-node.js:31:216)
at bc(/node_modules/firebase/lib/firebase-node.js:30:1259)
at Ii.h. Ib(/node_modules/firebase/lib/firebase-node.js:218:287)
在Qh.h.Jd(/node_modules/firebase/lib/firebase-node.js:185:251)
在Eh.Jd(/node_modules/firebase/lib/firebase-node.js:175:364)



<我注意到的一件事是,并不是所有的错误都被.catch()所捕获。我把Javascript Try Catch放在小提琴演示中,但它是一个单独的问题。



鉴于我在问题上遇到问题,我想知道是否有是我的节点安装有问题,所以已经重新安装节点,并删除并重新安装我的node_modules。我还没有取得任何进展。



有没有人在Node中使用新的Promise语法?有什么建议吗?

解决方案

我认为有3件事情正在进行:


  1. 节点v0.10.28没有带有Promise的内置实现。
  2. firebase模块捆绑了一个Promise / A +兼容的实现, t已经定义了一个。

  3. 我们提供的firebase实现是纯Promise / A +实现,不包含 .catch()

然而 .catch()非常有用, Promise / A +是半标准的,所以我们打算继续在即将发布的版本中将其添加到我们捆绑的Promise实现中,这可以解决您的问题。



在此期间,您可以执行以下任何操作:


  • 使用 .then(null,function(err ){...})而不是 .catch(function(err){...})

  • 将合适的Promise实施导入到您的e nvironment。例如。 global.Promise = require('rsvp')。Promise;

  • 升级到节点v0.12.x或更新版本内置Promise实现(支持 .catch())。


I am having some issues with error callbacks from Firebase Web in a Node application.

The original issue was that the error callback from a .set was apparently not being fired when I expected it to be.

In the process of debugging, I upgraded the Firebase npm module to the latest 2.4.0 (NodeJS 0.10.28) and then noticed that there is a new Promise syntax for the asynchronous calls.

I didn't find an example of using the new syntax, but made a fiddle and it seems to work in the way I expected in the browser.

var fb = new Firebase("https://katowulf-examples.firebaseio.com/");

// monitors changes and updates UI
try {
  fb.child('sorted_widgets').orderByChild('name').equalTo('one').once('value').then(handleSuccess).catch(handleError)
  fb.child(undefined).once('value').then(handleSuccess).catch(handleError)
  fb.child('sorted_widgets').set(undefined).then(handleSuccess).catch(handleError)

} catch (err) {
  console.error('Caught an error', err)
}

function handleSuccess(snap) {
  console.log('snap: ', snap.val())
}

function handleError(err) {
  console.log('error:', err)
}

I am now trying to use that syntax in my Node application and getting this error:

TypeError: Object #<nb> has no method 'catch'
    at myFunction (/node_scripts/my_script.js:514:19)
    at c (/node_modules/firebase/lib/firebase-node.js:238:58)
    at /node_modules/firebase/lib/firebase-node.js:199:710
    at ec (/node_modules/firebase/lib/firebase-node.js:52:165)
    at ac (/node_modules/firebase/lib/firebase-node.js:31:216)
    at bc (/node_modules/firebase/lib/firebase-node.js:30:1259)
    at Ii.h.Ib (/node_modules/firebase/lib/firebase-node.js:218:287)
    at Qh.h.Jd (/node_modules/firebase/lib/firebase-node.js:185:251)
    at Eh.Jd (/node_modules/firebase/lib/firebase-node.js:175:364)

One thing I noticed in fiddling is that not all errors are caught by .catch(). I put the Javascript Try Catch in the fiddle to demonstrate that, but it's really a separate issue.

Given that I am experiencing problems on top of problems, I wondered if there is something wrong with my Node installation, so have reinstalled Node and deleted and reinstalled my node_modules. I'm still not making any progress.

Has anybody got the new Promise syntax working in Node? Any suggestions please?

解决方案

I think there are 3 things going on:

  1. node v0.10.28 doesn't come with a builtin implementation of Promise.
  2. The firebase module bundles a Promise/A+-compatible implementation that it uses when there isn't already one defined.
  3. The implementation we ship with firebase is a pure Promise/A+ implementation and doesn't include .catch().

However .catch() is very useful and although not part of Promise/A+ it is semi-standard so we're planning to go ahead and add it to our bundled Promise implementation in an upcoming release, which should resolve your issue.

In the meantime, you could do any of the following:

  • Use .then(null, function(err) { ... }) instead of .catch(function(err) { ... })
  • Import a suitable Promise implementation into your environment. E.g. global.Promise = require('rsvp').Promise;
  • Upgrade to node v0.12.x or newer, which comes with a builtin Promise implementation (that supports .catch()).

这篇关于Firebase Web 2.4.0承诺Nodejs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆