Javascript 在NodeJS中使用蓝鸟承诺的链函数
很抱歉,我又问了一个关于承诺的问题,但是,我不能完全理解它,在阅读了大量关于这里的问题和解释之后,我仍然在挣扎 所以在我的nodejs项目中,我尝试做三件事 1) 从Facebook API获取用户信息Javascript 在NodeJS中使用蓝鸟承诺的链函数,javascript,node.js,promise,bluebird,Javascript,Node.js,Promise,Bluebird,很抱歉,我又问了一个关于承诺的问题,但是,我不能完全理解它,在阅读了大量关于这里的问题和解释之后,我仍然在挣扎 所以在我的nodejs项目中,我尝试做三件事 1) 从Facebook API获取用户信息 graph.get(message.user, function getUserInfo(err, res) { console.log(res) } 2) 从另一个API获取用户列表 request.get('https://api-url/api/users', {
graph.get(message.user, function getUserInfo(err, res) {
console.log(res)
}
2) 从另一个API获取用户列表
request.get('https://api-url/api/users', {
'auth': {
'bearer': 'bearerAuth'
}
})
3) 检查来自Facebook用户的名称是否与我从API返回的JSON数据中的名称匹配,然后将其交给用户
let aPieceOfData = "";
Bluebird.promisifyAll(graph.get(message.user))
.then(function(res) {
// this should give me the response from the Facebook API which is the user
// Then pass the response to the next .then(function(){})
})
.then(function(res) {
request.get('https://api-url/api/users', {
'auth': {
'bearer': 'bearerAuth'
}
const apiData = JSON.parse(response.body);
for (i in apiData) {
if (res.username == apiData[i].username) {
// if the username matches the user name then save some data to a variable outside this scope so I can access it
aPieceOfData = apiData[i].item;
}
}
})
})
.catch(function(err) {
console.log(err, "<<<<<<<");
})
让aPieceOfData=”“;
Bluebird.promisifyAll(graph.get(message.user))
.然后(功能(res){
//这应该会给我来自用户Facebook API的响应
//然后将响应传递给next.Then(function(){})
})
.然后(功能(res){
请求。获取('https://api-url/api/users', {
“auth”:{
“持票人”:“持票人”
}
const apiData=JSON.parse(response.body);
对于(apiData中的i){
if(res.username==apiData[i].username){
//如果用户名与用户名匹配,则将一些数据保存到此范围之外的变量,以便我可以访问它
aPieceOfData=apiData[i]。项目;
}
}
})
})
.catch(函数(err){
log(err,基于示例
我认为应该是这样的:
var g = Bluebird.promisifyAll(graph);
g.getAsync(message.user)
.then(function (res) {
// this should give me the response from the Facebook API which is the user
// Then pass the response to the next .then(function(){})
return res;
})
.then(function (res) {
return request.get('https://api-url/api/users', {
'auth': {
'bearer': 'bearerAuth'
}
});
})
.then(function (response) {
const apiData = JSON.parse(response.body);
for (i in apiData) {
if (res.username == apiData[i].username) {
// if the username matches the user name then save some data to a variable outside this scope so I can access it
aPieceOfData = apiData[i].item;
}
}
})
.catch(function (err) {
console.log(err, "<<<<<<<");
});
var g=Bluebird.promisifyAll(图);
g、 getAsync(message.user)
.然后(功能(res){
//这应该会给我来自用户Facebook API的响应
//然后将响应传递给next.Then(function(){})
返回res;
})
.然后(功能(res){
返回请求。获取('https://api-url/api/users', {
“auth”:{
“持票人”:“持票人”
}
});
})
.然后(功能(响应){
const apiData=JSON.parse(response.body);
对于(apiData中的i){
if(res.username==apiData[i].username){
//如果用户名与用户名匹配,则将一些数据保存到此范围之外的变量,以便我可以访问它
aPieceOfData=apiData[i]。项目;
}
}
})
.catch(函数(err){
console.log(err),“像这样试试吧”var g=Bluebird.promisifyAll(graph);g.get(message.user)。然后(…)
我得到了一个TypeError:g.get(…)。然后不是一个函数
我在上面包括了蓝鸟,尽管像这样…const Bluebird=require(“Bluebird”)
你能设置一个plunker吗?没有:)我一直在阅读一些关于承诺的内容,试图找出我遗漏了什么。因此,request.get方法的return语句返回的是承诺,而不是值。因此,在我的第三个中,我从res
获取值,而不是从response@SmurfEkko,当然,如果有帮助,你可以投票或接受答案:)。您也可以在我的第三个问题中更新您的问题,并提供有关So的详细信息。然后,我将从res而不是从响应中获取值
var g = Bluebird.promisifyAll(graph);
g.getAsync(message.user)
.then(function (res) {
// this should give me the response from the Facebook API which is the user
// Then pass the response to the next .then(function(){})
return res;
})
.then(function (res) {
return request.get('https://api-url/api/users', {
'auth': {
'bearer': 'bearerAuth'
}
});
})
.then(function (response) {
const apiData = JSON.parse(response.body);
for (i in apiData) {
if (res.username == apiData[i].username) {
// if the username matches the user name then save some data to a variable outside this scope so I can access it
aPieceOfData = apiData[i].item;
}
}
})
.catch(function (err) {
console.log(err, "<<<<<<<");
});