简体   繁体   English

明确承诺不起作用

[英]Express promise doesn't work

I am quite new in promises and I think I don't understand it correctly because I am trying it in my code without success. 我对诺言很陌生,我想我不太理解它,因为我在我的代码中尝试不成功。

I have a server on NodeJS, using Express library and express-promise 我在NodeJS上有一台服务器,使用Express库和Express-Promise

var express = require('express');
var app = express();
app.use(require('express-promise')());

Then I am handling ajax query: 然后我正在处理ajax查询:

var promise = function() {
    for(var query in req.query ){
        console.log( 'query: ', query );
        switch( query ){
           case 'getobserved':
             results.observedTags = getObserved();
             break; 
           ...
        }
    }
};

And getObserved is getting data from Firebase DB 而且getObserved从Firebase DB获取数据

var getObserved = function() {
    var observedTags = dbRef.child('observedTags');
    observedTags.on('value', function(snap) {
        var allObservedItems = snap.val();
        var list = [];
        for(var ii in allObservedItems ){
            list.push( allObservedItems[ii].name );
        }
        return list;
    });
};

And finally I am trying to send response to client by: 最后,我尝试通过以下方式向客户发送回复:

promise.then( res.send( results ), someRejectedMethod );

And I get in console this: 我进入控制台:

TypeError: undefined is not a function
at d:\wamp\www\soz2\server.js:100:13

Probably method "promise" is undefined. 方法“ promise”可能未定义。 I am not sure if I use express-promise wrong, or it's just my lack of knowledge about whole promises. 我不确定是否使用表达承诺的错误,还是我对整体承诺的了解不足。 Need some hint, please 需要一些提示,请

Probably method "promise" is undefined. 方法“ promise”可能未定义。

No, the promise function seems to be defined. 不, promise函数似乎已定义。 Only it is a function, not a promise object, and has no .then method. 只是它是一个函数,不是promise对象,并且没有.then方法。

I am not sure if I use express-promise wrong 我不确定我是否使用快递承诺错误

Yes. 是。 From how I understand their docs , all it seems to do is make res.send , res.json , and res.render accept promises or objects that contain promises, which are then recursively expanded and awaited. 从我对他们的文档的了解来看,它似乎要做的就是让res.sendres.jsonres.render接受promise或包含promise的对象,然后递归扩展并等待它们。

or it's just my lack of knowledge about whole promises. 或仅仅是我缺乏关于全部承诺的知识。

That as well. 那也一样。 I recommend the general promise resources as an entry point to learn about promises. 我推荐一般的诺言资源作为了解诺言的切入点。

First, you will have to promisify your Firebase methods so that getObserved does return a promise. 首先,您将不得不分配 Firebase方法,以便getObserved确实返回承诺。 (Currently, it seems to fail because you try to return from an async callback ). (当前,它似乎失败了,因为您尝试从异步回调return )。 You'll probably have to choose a promise libary if you aren't already using one. 如果尚未使用promise库,则可能必须选择一个诺言库。

Then, in your route you can use it like 然后,在您的路线中,您可以像

for (var query in req.query) {
    console.log('query: ', query);
    switch(query) {
        case 'getobserved':
            results.observedTags = getObserved(); // a promise
            break; 
         …
    }
}
res.json( result ); // here, express-promise does some magic

No need for the var promise = function() { line. 不需要var promise = function() {行。 And you probably can scrap promise.then( res.send( results ), someRejectedMethod ); 您可能会废弃promise.then( res.send( results ), someRejectedMethod ); as well, as express-promise does handle this for you. 同样,快速承诺确实会为您解决这个问题。 If you didn't use it, you could have done it explicitly with something like 如果您不使用它,则可以使用类似

var promise = getObserved();
promise.then(function(list) {
    res.json({observedTags: list});
}, someRejectedMethod);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM