javascript - 在 Lambda 中的 node8.10 中获取 API 调用导致 Promise和未定义
问题描述
我有一个用 Node8.1 编写的 Lambda 函数,我试图在其中获取一组对象(来自 Unsplash API 的服务器照片),然后将它们写入 DynamoDB。现在我无法得到我的 API 调用的结果,尽管链接承诺。任何帮助将不胜感激。
我已经尝试在我的函数中链接承诺以及异步/等待,但不断收到以下错误:
Promise {
<pending>,
...
和
TypeError: Cannot read property 'results' of undefined
at unsplash.search.photos.then.then.photos (/Users/stackery/Code/dynamodb-to-ses/src/writeToTable/index.js:19:12)
at process._tickCallback (internal/process/next_tick.js:68:7)
这是我的功能:
function getPhotos () {
// get items from the unsplash api
return unsplash.search.photos('servers')
.then( photos => {
toJson(photos)
})
.then( photos => {
// filter out restaurant servers - that's not what we're going for
photos.results.filter( photo => {
return photo.description.includes('computer') || photo.alt_description.includes('computer') || photo.description.includes('data') || photo.alt_description.includes('data');
}).then( photos => {
return photos;
});
}).catch ( error => {
console.log('Error getting photos');
console.log(error);
});
}
exports.handler = async () => {
const results = await getPhotos();
(other function logic)
const response = {
statusCode: 200,
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(results)
};
return response;
};
我期望来自 Unsplash API 的一组对象(我的凭据未显示,但它们是正确的,我可以访问 API)。
* *****编辑:***** *
这是我的相同功能的版本,带有 async/await (我更喜欢使用):
async function getPhotos () {
// get items from the unsplash api
try {
const photos = await unsplash.search.photos('servers', 1, 500); // get all 300+ photos
const json = await toJson(photos);
console.log(json); // this is working now, but filtering is not
const serverPhotos = json;
// filter out restaurant servers - that's not what we're going for
return serverPhotos.results.filter(photo => {
return photo.description.toLowerCase().includes('computer') || photo.alt_description.toLowerCase().includes('computer') || photo.description.toLowerCase().includes('data') || photo.alt_description.toLowerCase().includes('data') || photo.description.toLowerCase().includes('network');
});
}
catch (error) {
console.log('Error getting photos');
console.log(error);
}
}
exports.handler = async () => {
const results = await getPhotos();
...
return response;
};
它也不起作用,并且失败并出现以下错误:
Error getting photos
TypeError: Cannot read property 'filter' of undefined
at getPhotos (/Users/stackery/Code/dynamodb-to-ses/src/writeToTable/index.js:18:18)
at process._tickCallback (internal/process/next_tick.js:68:7)
这就是我所期待的——一组像这样的对象:
[{ id: '6vA8GCbbtL0',
created_at: '2019-03-14T13:39:20-04:00',
updated_at: '2019-03-19T15:01:00-04:00',
width: 3422,
height: 4278,
color: '#F8F7F7',
description: 'Computer interior',
alt_description: 'black and white computer tower',
urls: [Object],
links: [Object],
categories: [],
sponsored: false,
sponsored_by: null,
sponsored_impressions_id: null,
likes: 35,
liked_by_user: false,
current_user_collections: [],
user: [Object],
tags: [Array],
photo_tags: [Array] },
...
]
解决方案
这部分函数链不会返回Promise
对象,因为没有return
:
.then( photos => {
toJson(photos)
})
尝试将其更改为
.then( photos => {
return toJson(photos)
})
或者即使.then(toJson)
你感到雄心勃勃(:
否则,photos.results
不定义
推荐阅读
- mysql - Mysql2::Error "Access denied for user" 在 database.yml 中使用 env 变量
- angular - 通过 getElementById 以编程方式单击 mat-checkbox
- django-queryset - 2个值之间的查询集过滤器带有外键?
- javascript - 如何从 word javascript api 向 http:localhost 发出 GET 请求?
- android - 如何使用 GoogleMaps 绘制 Snake 路径?
- protractor - 如何在钩子后给予黄瓜优先顺序
- python - tkinter 是否包含在 python3 debian 包中?
- c# - How to rename migrations table in entity framework?
- javascript - 为创建通用过滤器完成了多次迭代
- reactjs - Dispatch.dispatch(...):无法在调度过程中调度