Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Response from API.get is not what is expected with queryStringParameters #1244

Closed
michaelcuneo opened this issue Jul 16, 2018 · 5 comments
Closed
Assignees
Labels
API Related to REST API issues

Comments

@michaelcuneo
Copy link

Do you want to request a feature or report a bug?

Confusion rather than a feature of bug.

What is the current behavior?

I have aws-amplify set up in my project alongside awsmobile.

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem. Your bug will get fixed much faster if we can run your code and it doesn't have dependencies other than AWS Amplify.

I have set the app up as per instructions, documentation, and examples. But using queryStringParameters does not work. I have the following to my cloud-api app in AWSMobilehub

const awsServerlessExpressMiddleware = require('aws-serverless-express/middleware');

var app = express()
app.use(awsServerlessExpressMiddleware.eventContext({ deleteHeaders: false }), bodyParser.json(), function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*")
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")
  next()
});

and also

app.get('/imageSets', function(req, res) {
  var params = {
    TableName: tableName,
    Select: 'ALL_ATTRIBUTES',
  };
  dynamodb.scan(params, (err, data) => {
    if (err) {
      res.json({error: 'Could not load items: ' + err.message});
    }
    res.json({
      data: data.Items.map(item => {
        return item;
      })
    });
  });
});

What is the expected behavior?

I don't get any errors from my query when I add a queryStringParameter inside the options... but it responds by returning my entire set instead of the ones I asked for...
In my following example. I have an attribute named status, with values active, inactive, completed.
The following code returns my entire result set, not just the active status items.

    API.get('imageSetsCRUD', '/imageSets', {
      queryStringParameters: {
        status: 'active',
      },
    })

Which versions of Amplify, and which browser / OS are affected by this issue? Did this work in previous versions?

aws-amplify 1.0.0
aws-amplify-react 1.0.0
awsmobile-cli 1.1.4

@michaelcuneo
Copy link
Author

I just realised I was working on old documentation, and have changed my project to suit the new documentation. I now get a result back from the API.get comment written in this manner.

API.get('imageSetsCRUD', '/imageSets?status=active').then((response) => ... etc.

And now response is a resource, filled with tonnes of information, but no results that I wanted.

{resource: "/imageSets", path: "/imageSets", httpMethod: "GET",…}
headers: {Accept: "application/json, text/plain, */*", Accept-Encoding: "gzip, deflate, br",…}
httpMethod: "GET"
isBase64Encoded: false
path: "/imageSets"
pathParameters: null
queryStringParameters: {status: "active"}
requestContext: {resourceId: "vajrrh", resourcePath: "/imageSets", httpMethod: "GET",…}
resource: "/imageSets"
stageVariables: {stage: "Development"}

@elorzafe
Copy link
Contributor

@michaelcuneo

On your lambda function you need to add FilterExpression. I did this. (The column was Years on my case)

app.get('/AWSMobileTable', function (req, res) {

    let scanParams = {
      TableName: tableName,
      FilterExpression: 'Years = :this_years',
      ExpressionAttributeValues: { ':this_years': req.query.years }
    }

    dynamodb.scan(scanParams, (err, data) => {
      if (err) {
        res.json({ error: 'Could not load items: ' + err });
        return;
      } else {
        res.json(data.Items);
        return;
      }
    });
  } 
});

and then I invoke like this

API.get('AWSMobileTableCRUD', '/AWSMobileTable?years=1984');

@michaelcuneo
Copy link
Author

Ahh thanks. Why can't documentation refer to things like this... I haven't seen the words 'FilterExpression' anywhere in any documents or forums or discussions. This works perfectly.

@elorzafe
Copy link
Contributor

elorzafe commented Aug 1, 2018

Cool

@elorzafe elorzafe closed this as completed Aug 1, 2018
@github-actions
Copy link

This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels or Discussions for those types of questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
API Related to REST API issues
Projects
None yet
Development

No branches or pull requests

2 participants