2015-07-01 22 views
4

我有我已經聲明爲模型,如下所示串的一個領域:Dynamo DB +把項目請求如何傳遞空值?

App.Student= DS.Model.extend({ 
name: DS.attr('string'), 
address1: DS.attr('string'), 
address2: DS.attr('string'), 
city: DS.attr('string'), 
state: DS.attr('string'), 
postal: DS.attr('string'), 
country: DS.attr('string'), 
}); 

在這裏,在編輯模式下,當我更新Adderess 2場然後在下面的錯誤爲空來了:

"Failed to edit property: One or more parameter values were invalid: An AttributeValue may not contain an empty string"

我知道會產生這個錯誤是因爲我更新地址2字段爲空,並且我想要什麼地址2場是不是強制性的(一個可以傳遞數據或者也可以離開那個欄爲空白「)

回答

1

最後我得到了這個權利,方式可能是一些什麼不同,但它爲我工作!

以下是相同的代碼。

AttributeUpdates: { 
        Address2: 
        { 
         Action: "DELETE" 
        }, 
        } 

然後我將條件的值加上。

if (propertyObj.address2) { 
     params.AttributeUpdates.address2 = { 
      Value: { 
       S: propertyObj.address2 
      }, 
      Action: "PUT" 
     } 
    } 

謝謝所有從我的心臟底部:)誰試圖幫助我,乾杯!

1

您可以使用AttributeValue null類型作爲佔位符。如果你不能改變類型,那麼選擇一個標記值,比如「ZZZZZ」或類似的,並使它代表空字符串。或者,您的模型可能只有一個地址字段,然後,如果第二個地址行爲空/空,則可以在地址1中編碼兩個地址行。

+0

它已經聲明爲一個字符串,我也不能使其「屬性Null值」是否有任何其他的方式做同樣的,其中的數據類型是一樣的「字符串」,我可以做手術? – Developer

+1

我更新了我的答案以解決您的問題。 –

1

我只是通過了空值。這裏是我的NodeJS腳本,用於將數據從文件加載到DynamoDB中,我只是使用三元組來避免出現空字符串。

var AWS = require("aws-sdk"); 
var fs = require('fs'); 

// Make sure we got a filename on the command line. 
if (process.argv.length < 3) { 
    console.log('Usage: node ' + process.argv[1] + ' FILENAME'); 
    process.exit(1); 
} 

var filename = process.argv[2]; 

AWS.config.update({ 
    region: "us-west-2", 
    //endpoint: "http://localhost:8000" 
}); 

var docClient = new AWS.DynamoDB.DocumentClient(); 

console.log("Importing Addresses into DynamoDB. Please wait."); 

var allObjs = JSON.parse(fs.readFileSync(filename, 'utf8')); 
allObjs.forEach(function(obj) { 
    var params = { 
     TableName: "Address", 
     Item: { 
      "address_detail_pid": obj.address_detail_pid, 
      "flat_type": obj.flat_type == '' ? null : obj.flat_type, 
      "flat_number": obj.flat_number == '' ? null : obj.flat_number, 
      "level_type": obj.level_type == '' ? null : obj.level_type, 
      "level_number": obj.level_number == '' ? null : obj.level_number, 
      "number_first": obj.number_first == '' ? null : obj.number_first, 
      "street_name": obj.street_name == '' ? null : obj.street_name, 
      "street_type_code": obj.street_type_code == '' ? null : obj.street_type_code, 
      "locality_name": obj.locality_name == '' ? null : obj.locality_name, 
      "state_abbreviation": obj.state_abbreviation == '' ? null : obj.state_abbreviation, 
      "postcode": obj.postcode == '' ? null : obj.postcode, 
      "longitude": obj.longitude == '' ? null : obj.longitude, 
      "latitude": obj.latitude == '' ? null : obj.latitude 
     } 
    }; 

    docClient.put(params, function(err, data) { 
     if (err) { 
      console.error("Unable to add obj", obj.address_detail_pid, ". Error JSON:", JSON.stringify(err, null, 2)); 
     } else { 
      console.log("PutItem succeeded:", obj.address_detail_pid); 
     } 
    }); 
}); 
+1

您的答案應該是處理空字符串/值的事實上的解決方案。然而,我的理解是,對於'DocumentClient.put'而不是'PutItem',這是允許的。對於其他人,請參閱http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html – user1322092

+0

一個警告,我無法查詢GSI,其中包括字段設置爲null作爲範圍值。 – user1322092