admin管理员组

文章数量:1435859

I have a NodeJS function in Lambda which calls out to a library in order to run a geospatial query in Dynamo DB.

Ultimately, I would like the results from this query to be returned by Lambda, as this Lambda function will ultimately be invoked by another and as such the results must be returned.

I am unable to return the results of the promise in a Lambda function.

I've tried rewriting the code several times, understanding promises, using async await... I've read a number of articles including .10/ .html Getting API call in node8.10 in Lambda results in Promise <pending> and undefined

To no avail.

const AWS = require('aws-sdk');
const ddb = new AWS.DynamoDB();
const ddbGeo = require('dynamodb-geo');
const config = new ddbGeo.GeoDataManagerConfiguration(ddb, 'MyGeoTable');
const myGeoTableManager = new ddbGeo.GeoDataManager(config);

exports.handler = async function (event, context) {
    let data = await myGeoTableManager.queryRadius({
        RadiusInMeter: 1000,
        CenterPoint: {latitude: 51.50, longitude: -0.17}
    });
    console.log(data);
    return data;
}

The code runs, but Lambda returns [] as the result.

I have a NodeJS function in Lambda which calls out to a library in order to run a geospatial query in Dynamo DB.

Ultimately, I would like the results from this query to be returned by Lambda, as this Lambda function will ultimately be invoked by another and as such the results must be returned.

I am unable to return the results of the promise in a Lambda function.

I've tried rewriting the code several times, understanding promises, using async await... I've read a number of articles including https://dashbird.io/blog/aws-lambda-supports-node-version-8.10/ https://techsparx./software-development/aws/aws-sdk-promises.html https://medium./tensult/async-await-on-aws-lambda-function-for-nodejs-2783febbccd9 Getting API call in node8.10 in Lambda results in Promise <pending> and undefined

To no avail.

const AWS = require('aws-sdk');
const ddb = new AWS.DynamoDB();
const ddbGeo = require('dynamodb-geo');
const config = new ddbGeo.GeoDataManagerConfiguration(ddb, 'MyGeoTable');
const myGeoTableManager = new ddbGeo.GeoDataManager(config);

exports.handler = async function (event, context) {
    let data = await myGeoTableManager.queryRadius({
        RadiusInMeter: 1000,
        CenterPoint: {latitude: 51.50, longitude: -0.17}
    });
    console.log(data);
    return data;
}

The code runs, but Lambda returns [] as the result.

Share Improve this question edited Jun 14, 2019 at 3:10 John Rotenstein 271k28 gold badges448 silver badges532 bronze badges asked Jun 13, 2019 at 10:33 Ben FrekeBen Freke 811 silver badge5 bronze badges 3
  • Potential duplicate of stackoverflow./questions/14220321/… – horia Commented Jun 13, 2019 at 10:41
  • Wouldn't it be caused by data being really an empty set, i.e. there's no data matching your query in DDB? – Stock Overflaw Commented Jun 13, 2019 at 11:59
  • No, if I run a .then() on the promise and console.log it, I get the response I'm expecting. – Ben Freke Commented Jun 13, 2019 at 12:01
Add a ment  | 

2 Answers 2

Reset to default 4

I figured it out! Numpty moment.

According to AWS, to get the results of a promise in Lambda you need to return the promise. So changing my code to:

const AWS = require('aws-sdk');
AWS.config.loadFromPath('./config.json');
const ddb = new AWS.DynamoDB();
const ddbGeo = require('dynamodb-geo');
const config = new ddbGeo.GeoDataManagerConfiguration(ddb, 'MyGeoTable');
const myGeoTableManager = new ddbGeo.GeoDataManager(config);

exports.handler = function (event, context) {
    return myGeoTableManager.queryRadius({
        RadiusInMeter: 1000,
        CenterPoint: {latitude: 51.50, longitude: -0.17}
    });
};

Solved it.

Try as I might, I just cannot see an issue with your code. If .then(data => console.log(data) works, then your code really should work. Perhaps it would be more helpful for me to leave this question to someone who can see something that I cannot.. But in the meantime, you can probably get it to work using the callback style of lambda invocation. Maybe it'll even help reveal another detail of the issue...

const AWS = require('aws-sdk');
const ddb = new AWS.DynamoDB();
const ddbGeo = require('dynamodb-geo');
const config = new ddbGeo.GeoDataManagerConfiguration(ddb, 'MyGeoTable');
const myGeoTableManager = new ddbGeo.GeoDataManager(config);

exports.handler = function (event, context, callback) {
    myGeoTableManager.queryRadius({
        RadiusInMeter: 1000,
        CenterPoint: {latitude: 51.50, longitude: -0.17}
    })
        .then(data => {
            console.log(data);
            callback(null, data);
        });
}

本文标签: javascriptHow do I ultimately return the value of a promise in a Lambda functionStack Overflow