[英]AWS Dynamodb not fetching data synchronously by node js
I am new on node js dynamo db I wrote a node js sdk to fetch one row from a table ona dynamodb. 我是node js dynamo db的新手,我写了一个node js sdk来从表dynamodb上获取一行。 It is fetching data correctly but not immediately for this I got error 它正在正确获取数据,但不是立即获取此错误
My code is below a simple code 我的代码在一个简单的代码下面
var AWS = require("aws-sdk");
var config = function(){
AWS.config.update({region: 'us-east-1'});
// Create the DynamoDB service object
var ddb = new AWS.DynamoDB({apiVersion: '2012-08-10'});
var params = {
TableName: 'tblConfigs',
// Key: {
// "id" : {S: "1"},
// }
ExpressionAttributeValues: {
":v1": {
S: "1"
}
},
FilterExpression: "id = :v1",
};
var v;
var json = ddb.scan(params, function(err, data) {
if (err) {
console.log("Error", err);
} else {
v = data;
// console.log(JSON.stringify(data.Item));
// return JSON.stringify(data.Item);
}
});
// if(v=="u")
// for(var i=0;)
v = v.Items[0];
// for()
var con = {
"host": v.endpoint.S,
"user": v.endpoint.username.S,
"password": v.endpoint.password.S,
"database": v.endpoint.database_name.S
};
return con;
}
And I got the below error 我得到了以下错误
> config()
TypeError: Cannot read property 'Items' of undefined
at config (repl:31:7)
as v is undefined so it is giving the error but v is not undefined when I execute the code in node console it first time gave undefined next time it gave value 由于v是未定义的,所以它给出了错误,但是当我在节点控制台中执行代码时v不是未定义的,它是第一次给出undefined,下次是给出值
like below 像下面
> v
{ Items:
[ { password: [Object],
stage: [Object],
username: [Object],
id: [Object],
endpoint: [Object],
database_name: [Object] } ],
Count: 1,
ScannedCount: 1 }
how can I fetch the row immediately not after some time? 一段时间后如何立即获取行? IS there any good way in dynamodb I tried, get, getItem, scan, query all are giving data correctly but not immediately...Please suggest 我尝试过的dynamodb中有什么好方法吗,get,getItem,扫描,查询都正确地提供了数据但不是立即...请建议
You are missing one important thing: Javascript execution is asynchronous. 您缺少一件事:Java语言执行是异步的。 As long as you are not using async/await
syntax you have to "play" with callbacks like this: 只要您不使用async/await
语法,就必须像下面这样“播放”回调:
var ddb = new AWS.DynamoDB({ apiVersion: '2012-08-10' });
function loadConfig(callback) {
var params = {
TableName: 'tblConfigs',
ExpressionAttributeValues: {
':v1': {
S: '1'
}
},
FilterExpression: 'id = :v1'
};
ddb.scan(params, function (error, data) {
if (error) {
callback(error);
} else {
var item = data.Items[0];
callback(null, {
'host': item.endpoint.S,
'user': item.endpoint.username.S,
'password': item.endpoint.password.S,
'database': item.endpoint.database_name.S
});
}
});
}
loadConfig(function (error, configuration) {
if (error) {
console.log(error);
} else {
// Your connection logic (JUST AN EXAMPLE!)
var connection = mysql.connect({
host: configuration.host,
user: configuration.user,
password: configuration.password,
database: configuration.database
})
}
});
Btw. 顺便说一句。 storing database configurations in DynamoDB isn't a good solution, i would recommend to check AWS Systems Manager Parameter Store
. 在DynamoDB中存储数据库配置不是一个好的解决方案,我建议您检查AWS Systems Manager Parameter Store
。
Edit 编辑
To give you a short example how the async/await
syntax looks like 给你一个简短的例子, async/await
语法看起来像
var ddb = new AWS.DynamoDB({ apiVersion: '2012-08-10' });
const loadConfig = async () => {
const { Items } = await ddb.scan({
TableName: 'tblConfigs',
ExpressionAttributeValues: {
':v1': {
S: '1'
}
},
FilterExpression: 'id = :v1'
}).promise();
const item = Items[0];
return {
'host': item.endpoint.S,
'user': item.endpoint.username.S,
'password': item.endpoint.password.S,
'database': item.endpoint.database_name.S
};
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.