1

When trying to place a new entry to the table it misses the execution of the put function. Fuuny, it used to work, and now for whichever reasons it doesn't. allValues is an array, and returns the data fully. Whenever trying to call saveData it does execute everything apart put function.

For some reason, I have tried it a few times, and it worked fine, but suddenly it doesn't place the entry anymore. Tried to play around, but can't seem to find an issue? Might be caused by asynchronous?

//Amazon sdk
let AWS = require("aws-sdk");
let db = require('database');
//Amazon config
AWS.config.update({
    region: "us-east-1",
    endpoint: "https://dynamodb.us-east-1.amazonaws.com"
});

//DynamoDb client
var docClient = new AWS.DynamoDB.DocumentClient();

//Axios will handle HTTP requests to web service
let axios = require ('axios');

//The ID of the student's data that I will download
let requestID= '*********';

//URL where student data is available
let url = '*************************';

exports.handler = async (event) => {
        //Get synthetic data
        let allValues = (await axios.get(url + requestID)).data.target;
        let allStart = (await axios.get(url + requestID)).data.start;

        for(let i = 0; i < allValues.length; i++){
            //db.saveData(i, allValues[i], allStart);
        }
        db.saveData();
};

and my database.js

et AWS = require("aws-sdk");

//Create new DocumentClient
let documentClient = new AWS.DynamoDB.DocumentClient();

//Returns all of the connection IDs
//module.exports.saveData = async (sId, sValue, sTime) => {
module.exports.saveData = async () => {
     let params = {
        TableName: "SynData",
        Item:{
            "synId": 66,
            "synValue": 1579.21,
            "synTime": "2019-01-01"
        }
    };
    //return documentClient.put(params).promise();
    documentClient.put(params, function(err, data) {
    if (err) {
        console.error("Unable to add item. Error JSON:", JSON.stringify(err, null, 2));
    } else {
        console.log("Added item:", JSON.stringify(data, null, 2));
    }
});
};

The logs do not show any return for put function.

1 Answer 1

4

saveData() is async, therefore it returns a promise. You need to await on it. Also, I don't understand why you gave up on the .promise() call. Using callbacks is outdated and much more convoluted.

Your code should look like this:

module.exports.saveData = async () => {
     let params = {
        TableName: "SynData",
        Item:{
            "synId": 66,
            "synValue": 1579.21,
            "synTime": "2019-01-01"
        }
    };
    return await documentClient.put(params).promise();
};

On your client, just await on saveData():

for(let i = 0; i < allValues.length; i++){
   await db.saveData(i, allValues[i], allStart);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.