I am building a React.js app in which I display some data hosted on AWS DynamoDB. I wrote a module called getList.js which makes a request to AWS DynamoDB to get the data. When I test this module using "node getList.js" it returns the expected data, no problem. [Should note, I have stored my AWS credentials as recommended in .aws/credentials] Here is the code for my getList.js module:
var AWS = require("aws-sdk");
AWS.config.region = region;
module.exports = getList;
function getList (callback) {
const documentClient = new AWS.DynamoDB.DocumentClient();
var params = {
TableName: "tableName"
};
documentClient.scan(params, function(err, data) {
if (err) {
console.log("ERROR", err)
}
else {
//get data, etc, return in callback
callback(data);
}
});
}
Now I am trying to call this module in my React app, as shown below:
import React, { Component } from 'react';
import './App.css';
let listProject = require('../../modules/getList')
class App extends Component {
state = {
list: []
}
componentDidMount = () => {
listProject(function(res) {
this.setState({ list: res})
});
}
render() {
return (
<div className="App">
<div className="App-header">
<h2>Automaticity</h2>
</div>
<p className="App-intro">
{JSON.stringify(this.state.list)}
</p>
</div>
);
}
}
export default App;
However, when I start the React app in my browser, the app loads fine, but the data is never retrieved, and I get this error:
Error: Missing credentials in config (specifically the error occurs when getList.js calls documentClient.scan(params, function(err, data)
Am I missing some dependency or code that will make my setup work??
via followtheturk
No comments:
Post a Comment