2016-03-11 37 views
1

我測試了接口並做了一些更改,以便在後端使用我的sailsjs(版本11)。在這種情況下,我使用帆船服務,事情看起來很好,但我總是回到未決狀態。 ```一段時間後api調用sinch作爲承諾

SinchService.js 
var sinchAuth = require('sinch-auth'); 
var request = require('request'); 

var sinchMessaging = {}; 
var Promise = require('bluebird'); 
//sinchMessaging.sendMessage = function (phoneNumber, message) { 
//exports.sendMessage = function (phoneNumber, message) { 
module.exports = { 
sendMessage: function (phoneNumber, message) { 
var auth = sinchAuth(); 
if (!auth) { 
    throw new Error("No Authorization was provided"); 
} 
var options = { 
    method: 'POST', 
    url: "https://messagingApi.sinch.com/v1/sms/" + phoneNumber, 
    headers: { 
     "Content-Type": "application/json", 
     "Authorization": auth 
    }, 
    body: "{\"Message\":\"" + message + "\"}" 
    // body: {"Message" : message } 
}; 

return new Promise(function (resolve, reject) { 
    request(options, function (error, response, body) { 

     sails.log("Finished with call"); 

     if (error) { 
      sails.log(error); 
      throw error; 
     } 
     else { 
      sails.log("Finished with body ", body);//.MessageId 

      return resolve(response.body); 
     } 

    }); 

}) 
}, 


getStatus: function (messageId) { 
var auth = sinchAuth(); 
if (!auth) { 
    throw new Error("No Authorization was provided"); 
} 
var options = { 
    method: 'GET', 
    url: "https://messagingApi.sinch.com/v1/sms/" + messageId, 
    headers: { 
     "Content-Type": "application/json", 
     "Authorization": auth 
    } 
}; 
return new Promise(function (resolve, reject) { 
    request(options, function (error, response, body) { 

     sails.log("Finished with call"); 

     if (error) { 
      sails.log(error); 
      throw error; 
     } 
     else { 
      return resolve(response.body); 
     } 

    }); 

}) 

} 
}; 

```

agendaService.js 
var jsonObjS; 

SinchService.sendMessage(phoneNumber, message).then(function (results) { 
var jsonObj = JSON.parse(results); 
console.log('results sendMessage ', jsonObj.messageId); 
if (jsonObj.messageId!==undefined){ 
SinchService.getStatus(jsonObj.messageId).then(function (results_s) { 
    jsonObjS = JSON.parse(results_s); 
    console.log('results getStatusS ', jsonObjS.status); 

SinchService.getStatus(jsonObjS.messageId).then(function (results_s) { 
var jsonObjS = JSON.parse(results_s); 
console.log('results getStatusS ', jsonObjS.status); 
}); 
}); 

```

回答

0

等待永遠是第一的地位,再次查詢看到的消息的狀態。