Javascript 从承诺解析调用传回数据

Javascript 从承诺解析调用传回数据,javascript,node.js,promise,es6-promise,Javascript,Node.js,Promise,Es6 Promise,我试图调用一个函数,然后在返回的参数中返回一些数据,我正在使用Promissions,但无法返回返回的数据 我这里有电话: fbUserInfo(recipientId,req).then(function(){ getSessionData(FB_ID).then(function(rowsJson){ console.log('%%%%%%%%' + JSON.stringify(rowsJson)); }) }) 这里定义了我的函数 /* * FU

我试图调用一个函数,然后在返回的参数中返回一些数据,我正在使用Promissions,但无法返回返回的数据

我这里有电话:

  fbUserInfo(recipientId,req).then(function(){
    getSessionData(FB_ID).then(function(rowsJson){
       console.log('%%%%%%%%' + JSON.stringify(rowsJson));
    })
  })
这里定义了我的函数

/*
*  FUNCTION: getSessionData
*  PURPOSE : Get the session data into global variables
*/
function getSessionData(FB_ID){

  var gender, user_name;

  var rowsJson = {'gender': gender, 'user_name': user_name };

  console.log('START to get client data');


  return new Promise((resolve,reject) => {

    client.hmget(FB_ID,'gender',function(err,reply){
        rowsJson.gender = reply;
        console.log('^^^ gender :' + rowsJson.gender);

    });
    client.hmget(FB_ID,'name',function(err,reply){
        rowsJson.user_name = reply;
        console.log('^^^ user_name :' + rowsJson.user_name);
    });



    resolve(rowsJson);

  }); // return


} // getSessionData


/*
*  FUNCTION: fbUserInfo
*  PURPOSE : Called once when displaying the user welcome message
*            to get the Facebook user details an place them in the 
*            session if they dont already exist
*/
function fbUserInfo(id,req) {

  return new Promise((resolve, reject) => {

    var url = 'https://graph.facebook.com/v2.6/' + id; 
    console.log('^^^^ url ' + url);

    const options = {  
     method: 'GET',
    uri: url,
    qs: {
      fields: 'first_name,last_name,profile_pic,locale,timezone,gender',
      access_token: FB_PAGE_TOKEN1
    },
    json: true // JSON stringifies the body automatically
   }

   console.log('^^^ CALL RQ');
   rp(options)    
   .then((response) => {    

       console.log('Suucess' + JSON.stringify(response.gender));
       var profile_pic = JSON.stringify(response.profile_pic).replace(/\"/g, "");

       console.log('1. profile_pic:' + profile_pic);

        // Now find the position of the 6th '/'
       var startPos = nth_occurrence(profile_pic, '/', 6) + 1;
       console.log('1. Start Pos: ' + startPos);  

        // Find the position of the next '.'  
       var stopPos = profile_pic.indexOf(".",startPos) ;
       console.log('2. Stop Pos: ' + stopPos);

       FB_ID   = profile_pic.substring(startPos,stopPos);


       console.log('profile_pic :' + profile_pic);
       console.log('FB_ID :' + FB_ID); 

       var gender = JSON.stringify(response.gender).replace(/\"/g, "");


       var name = JSON.stringify(response.first_name).replace(/\"/g, "");
       console.log('name: ' + name);


        console.log('** Set session variable');



        client.exists("key",FB_ID, function (err, replies) {

        if(!err && replies===1){

          console.log("THE SESSION IS ALREADY PRESENT ");
        }else{
          console.log("THE SESSION DOES NOT EXIST");
          // Now create a session from this
          client.hmset(FB_ID, {
            'gender': gender,
            'name': name
          });

          resolve();
        }});

      })
      .catch((err) => {
       console.log(err)
       reject();
      });


    })
} // fbUserInfo
我得到的是:

1. profile_pic:https://scontent.xx.fbcdn.net/v/t1.0-1/p200x200/xxxxxxxxxx.jpg?oh=f43f7946f07e2bfb16fd0428da6a20e3&oe=5824B5F0
1. Start Pos: 48
2. Stop Pos: 96
profile_pic :https://scontent.xx.fbcdn.net/v/t1.0-1/p200x200/xxxxxxxxx.jpg?oh=f43f7946f07e2bfb16fd0428da6a20e3&oe=5824B5F0
FB_ID :13882352_10154200039865923_27612879517xxxxxxxx
name: Ethan
** Set session variable
^^^ gender :male
^^^ user_name :Ethan
THE SESSION DOES NOT EXIST
START to get client data
%%%%%%%%{}

您没有以正确的方式在promise中使用回调,并且在client.hmget函数完成之前,您的promise已解析。 您需要promise chain来获得您需要的,或者您可以像这样快速修复它:

return new Promise((resolve,reject) => {

    client.hmget(FB_ID,'gender',function(err,reply){

        if (err) {
           return reject(err);
        }

        rowsJson.gender = reply;
        console.log('^^^ gender :' + rowsJson.gender);

      client.hmget(FB_ID,'name',function(err,reply){

        if (err) {
           return reject(err);
        }

        rowsJson.user_name = reply;
        console.log('^^^ user_name :' + rowsJson.user_name);

        resolve(rowsJson);

      });

    });

  })

如果
client.hmget
是异步的,您将需要重写代码-因为您当前正在解析client.hmget之前的代码,甚至会调用回调函数以避免出现在
fbUserInfo
中@Bergi,我为什么要避免这个,还有什么选择呢?点击蓝色文本。这是一个链接。@user1907509:你在链接的线程中读到我的答案了吗?非常感谢,我差一点就找到了。我使用了正确的方法,即JSON传回多段数据吗?你使用的是javascript对象,而不是JSON(JSON只是一种文件格式)。由于您已在promise score之外声明了对象,因此可以这样使用它,但我建议您阅读更多有关使用promises的内容,因为这将大大简化异步代码的使用。我不知道
hmget
的作用,但您可能需要显式等待异步回调,而不仅仅是最后一次。