Skip to content

Instantly share code, notes, and snippets.

@JNaeemGitonga
Last active December 30, 2019 21:43
Show Gist options
  • Save JNaeemGitonga/d56c659b46287a148e2c413f2f272b88 to your computer and use it in GitHub Desktop.
Save JNaeemGitonga/d56c659b46287a148e2c413f2f272b88 to your computer and use it in GitHub Desktop.
func getStories(ctx context.Context) (events.APIGatewayProxyResponse, error) {
err := client.Ping(ctx, readpref.Primary())
if err != nil {
return handleError(err)
}
cursor, err := collection.Find(ctx, bson.D{})
defer cursor.Close(ctx)
if err != nil {
return handleError(err)
}
// stories is the array of stories that will get fetched
var stories []Story
for cursor.Next(ctx) {
// create a value into which the single document can be decoded
var story Story
err := cursor.Decode(&story)
if err != nil {
return handleError(err)
}
stories = append(stories, story)
}
return marshalJSONAndSend(stories)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment