我需要將用戶的信息存儲在DynamoDB中,並將郵件發送到同一用戶(如果它尚不存在於DynamoDB表中)。我正在爲循環做這件事。該列表只包含2條記錄。問題是隻有第二條記錄被插入到表中並且郵件被髮送兩次給同一個用戶。下面是代碼:NodeJS:在dynamodb中插入記錄(如果不存在)
module.exports.AddUser = function(req, res, usersList, departmentId) {
var _emailId = "";
var _userName = "";
var _departmentId = departmentId;
for (var i = 0; i < usersList.length; i++) {
_emailId = usersList[i].emailId;
_userName = usersList[i].userName;
var params = {
TableName: "UsersTable",
Key: {
"emailId": _emailId,
"departmentId": _departmentId
}
};
docClient.get(params, function(err, data) {
if (!err) {
if (!data.items)
AddUserAndSendEmail("UsersTable", _emailId, _userName);
//The above function is being called twice but for the same user.
//It has a check so not inserting the same record twice but
//sending two mails to the same user.
}
});
}
res.end("success");
}
function AddUserAndSendEmail(tableName, emailId, _userName) {
var params = {
TableName: tableName,
Item: {
"emailId": emailId,
"departmentId": 101//Default Department
}
};
docClient.put(params, function(err, data) {
if (!err) {
//Send Email Code Here
} else {
console.log("error");
}
});
}
有什麼能爲這種奇怪的行爲的原因是什麼?真的很沮喪,我即將放棄這一點。
其他條目的電子郵件地址是什麼?到目前爲止,代碼應該是正確的。 –
@HeadhunterXamd他們都不一樣。驗證。 – user1640256
也許檢查/顯示'AddUserAndSendEmail'後面的代碼?問題可能在於這個功能。 –