I am having difficulty to understand where is my query stored in which array variable. I know the "var params" contains all the query conditions and "db.query" will perform the actual query action. But how do I get my query result which match my search condition?
At the end, I want to pass my query output parameters (title, postedby) to html or jade for display so I need to understand where all the query data are stored at first place and how to retrieve them. Some sample code to demo this part would be greatly appreciated. Thanks
var params = {
TableName: tablename,
AttributesToGet: [
'title',
'postedBy'
],
Limit: 20,
ScanIndexForward: true,
KeyConditions: { /* required */
key : {
ComparisonOperator: 'EQ', /* required */
AttributeValueList: [
{
'N' : 'some value'
}
/* more items */
},
},
};
db.query(params, function(err, data) {
if (err) {
console.log('show error here');
} else {
console.log('Success');
}
});
I found out that all my query data is stored inside "data" from function(err, data). But i am having problem passing "data" to the following export function which later can be used as a local variable inside my webpage. Then I realized the "data" part is always empty {} inside export function even i am sure the above db.query can return the right result. So i believe somehow the export.list function is executed before the db.query happen so I am not getting any data assigned. How can I overcome this part? Thanks.
exports.list = function(req, res){
res.render('post', {title:'Title', posts : data });
console.log("Result: %j", data);
};
Note: Console.log("Result: ", data) is showing {}
I figured it out. Use the callback function due to the nature of javascript. Thanks.