简体   繁体   English

如何从节点中的api承诺返回对象

[英]How to return object from api promise in node

I'm trying to understand how promises work, and apparently missing something.我试图了解承诺是如何工作的,但显然错过了一些东西。

In node I have a search function that uses the Twit module to return Twitter results, then I want to do something with the result:在节点中,我有一个使用 Twit 模块返回 Twitter 结果的搜索功能,然后我想对结果做一些事情:

var twitter = require('../server/twit');

exports.getTwitSearchResult = function(query, cb) {
  var t = twitter.searchTwitter(query)

  // do something with variable 't'
  // var tweet = t.data.statuses
  // ...
  // ...

  cb(null, tweet);
};

twitter function at '../server/twit' : '../server/twit' twitter 功能:

var Twit = require('twit')
var bot = new Twit(config);

exports.searchTwitter = function(query){
  var tsearch = function(query, callback){

    return bot.get('search/tweets', {
      q: query
      , since: '2016-02-01'
      , result_type: 'popular'
      , count: 1
    }, function(err, data, response){
      callback(data)
    });
  };

  tsearch(query, function(callback){
    return callback
  })
};

The problem is that 't' always returns as undefined or [object Promise] .问题是 't' 总是返回为undefined[object Promise] I have tried several different ways of writing the functions using promises and callbacks, along the lines of something like this:我已经尝试了几种不同的使用承诺和回调来编写函数的方法,大致如下:

var twitter = require('../server/twit');

exports.getTwitSearchResult = function(query, cb) {
    var t = function(query, callback){

    return twitter.searchTwitter(query)
        .then(function(tweet){
            callback(tweet)
        })
    }

  // do something with variable 't'
  // var tweet = t.data.statuses
  // ...
  // ...

  cb(null, tweet);
};

But then I get TypeError: Cannot read property 'then' of undefined但是后来我得到了TypeError: Cannot read property 'then' of undefined

How can I write this better so that I can use the results returned from the Twit promise?我怎样才能写得更好,以便我可以使用从 Twit 承诺返回的结果?

Your variable t will always be undefined as the function is asynchronous and doesn't return anything.您的变量 t 将始终未定义,因为该函数是异步的并且不返回任何内容。 You need to use a callback instead.您需要改用回调。

For your '../server/twit' file:对于您的“../server/twit”文件:

var Twit = require('twit')
var bot = new Twit(config);

exports.searchTwitter = function(query, callback){
    bot.get('search/tweets', {
      q: query
      , since: '2016-02-01'
      , result_type: 'popular'
      , count: 1
    }, callback);
};

The main file:主文件:

var twitter = require('../server/twit');

exports.getTwitSearchResult = function(query, cb) {
  twitter.searchTwitter(query, function(err, data, response) {
    // do something with variable 'data'
    // var tweet = data.statuses
    // ...
    // ...

    cb(null, tweet);
  });   

};

If you want to use promises, you should switch to use the module twit-promise and change your code to this:如果你想使用 promises,你应该切换到使用模块twit-promise并将你的代码更改为:

var Twit = require('twit-promise')
var bot = new Twit(config)

exports.searchTwitter = function(query){
    return bot.get('search/tweets', {
      q: query
      , since: '2016-02-01'
      , result_type: 'popular'
      , count: 1
    });
};



var twitter = require('../server/twit');

exports.getTwitSearchResult = function(query, cb) {
  twitter.searchTwitter(query)
  .then(function(result) {
    // do something with variable 'result'
    // var tweet = result.data.statuses
    // ...
    // ...

    cb(null, tweet);
  })
  .catch(function(err) {
    cb(err);
  });   

};

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

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