DynamoDB - Get无效

时间:2016-01-15 11:14:27

标签: node.js amazon-web-services amazon-dynamodb

我使用名为dynamoose的库来访问dynamoDB。

https://github.com/automategreen/dynamoose

我定义了一个模型:

            <label class="control-label" for="DestinationPhoneNmber">Destination Phone Number*</label>
            <input class="input-validation-error form-control" data-val="true" data-val-required="Please enter the destination phone number!" id="DestinationPhoneNmber" name="DestinationPhoneNmber" placeholder="+919876543210" type="text" value="">
            <span class="field-validation-error" data-valmsg-for="DestinationPhoneNmber" data-valmsg-replace="true">Please enter the destination phone number!</span>

当我执行保存操作时,一切正常,数据保存到db。

var App = dynamoose.model('AppTest', new dynamoose.Schema ({
   appID: { type: String, hashkey: true },
   email: String,
   isMaster: Boolean,
   password: String
}));

然后,当我尝试获取保存的数据时,也没有错误,但用户未定义,尽管它存在于db中:

    var app = new App({ 
       appID: user.appID, 
       email: user.email,     
       isMaster: false, 
       password: user.pwd 
    });

    app.save(function (err) {
       // Data is saved, and no error here
    });

2 个答案:

答案 0 :(得分:1)

问题在于您如何访问DynamoDB中的记录。

'get'只能在主表键上使用。在这种情况下appID。

App.get({appID: 'abc'})

对于你的情况,由于电子邮件只是一个普通字段,你必须使用'scan'。

App.scan('email').eq('user@example.com').exec()

如果您将电子邮件定义为范围键,则可以使用带有appID和电子邮件的“get”

var App = dynamoose.model('AppTest', new dynamoose.Schema ({
   appID: { type: String, hashkey: true },
   email: { type: String, rangeKey: true },
   isMaster: Boolean,
   password: String
}));

App.get({appID: 'abc', email: 'user@example.com'})

如果将电子邮件定义为全局索引,则可以使用“查询”。

var App = dynamoose.model('AppTest', new dynamoose.Schema ({
   appID: { type: String, hashkey: true },
   email: { type: String, index: { global: true }},
   isMaster: Boolean,
   password: String
}));

App.query('email').eq('user@example.com').exec()

作为一个注释,查询和扫描返回数组。

答案 1 :(得分:0)

在DynamoDB中,您可以通过主键获取项目。在您的示例中,主键为Sub SaveWorksheetsAsCsv() Dim ws As Excel.Worksheet Dim SaveToDirectory As String Dim CurrentWorkbook As String Dim CurrentFormat As Long Dim cell As Range CurrentWorkbook = ThisWorkbook.FullName CurrentFormat = ThisWorkbook.FileFormat ' Store current details for the workbook SaveToDirectory = "C:\Temp\" For Each ws In ThisWorkbook.Worksheets 'This was a check on the worksheet name for excluding some worksheets that I didn't want to export 'If ws.name <> "Instructions" And ws.name <> "Parameters" And ws.name <> "BI Data & Worksheet" Then 'This makes the current sheet its own workbook so it can be saved. Sheets(ws.name).Copy 'Not sure what I was doing here 'For Each cell In [b:b] 'If cell.Value = "~" Then cell.ClearContents ''put any value you want here 'Next cell ActiveWorkbook.SaveAs filename:=SaveToDirectory & ws.name & ".csv", FileFormat:=xlCSV ActiveWorkbook.Close SaveChanges:=False ThisWorkbook.Activate 'End If Next Application.DisplayAlerts = False ThisWorkbook.SaveAs filename:=CurrentWorkbook, FileFormat:=CurrentFormat Application.DisplayAlerts = True ' Temporarily turn alerts off to prevent the user being prompted ' about overwriting the original file. End Sub 。您正在尝试使用电子邮件地址获取数据。您需要使用appId更改此项目。

如果您需要按appId获取项目,则应创建一个使用电子邮件地址作为哈希键的二级索引。然后,您可以通过查询二级索引按电子邮件地址获取数据。