2015-11-26 12 views
9

में एक संरचना होने के लिए नोट: ये सभी डायनेमो डीबी के स्थानीय उदाहरण पर हो रहे हैं।त्रुटि अमान्य पैरामीटर टाइप: अपेक्षित पैराम्स। Iemem ['pid'] DynamoDB

var params = { 
    TableName: "TABLE-NAME", 
    KeySchema: [ 
     { AttributeName: "pid", 
      KeyType: "HASH" 
     } 
    ], 
    AttributeDefinitions: [ 
     { AttributeName: "pid", 
      AttributeType: "S" 
     } 
    ], 
    ProvisionedThroughput: { 
     ReadCapacityUnits: 1, 
     WriteCapacityUnits: 1 
    } 
}; 


dynamodb.createTable(params, function(err, data) { 
    if (err) 
     console.log(JSON.stringify(err, null, 2)); 
    else 
     console.log(JSON.stringify(data, null, 2)); 
}); 

इस समारोह है कि डीबी में तत्वों (Node.js में) जोड़ने के लिए कहा जाता है किया जा रहा है:

इस कोड है कि मैं DynamoDB शैल से एक तालिका बनाने के लिए उपयोग किया है है:

function(request, response) { 
    params = { 
    TableName: 'TABLE-NAME', 
    Item: { 
     pid: 'abc123' 
    } 
    }; 
    console.log(params); 
    dynamodb.putItem(params, function(err, data) { 
    if (err) 
     console.log(JSON.stringify(err, null, 2)); 
    else 
     console.log(JSON.stringify(data, null, 2)); 
    }); 
} 

उत्पादन है कि मैं मिलता है:

{ TableName: 'TABLE-NAME', 
    Item: { pid: 'abc123' } } // THIS IS PARAMS 
{ 
    "message": "There were 7 validation errors:\n* InvalidParameterType: Expected params.Item['pid'] to be a structure\n* UnexpectedParameter: Unexpected key '0' found in params.Item['pid']\n* UnexpectedParameter: Unexpected key '1' found in params.Item['pid']\n* UnexpectedParameter: Unexpected key '2' found in params.Item['pid']\n* UnexpectedParameter: Unexpected key '3' found in params.Item['pid']\n* UnexpectedParameter: Unexpected key '4' found in params.Item['pid']\n* UnexpectedParameter: Unexpected key '5' found in params.Item['pid']", 
    "code": "MultipleValidationErrors", 
    "errors": [ 
    { 
     "message": "Expected params.Item['pid'] to be a structure", 
     "code": "InvalidParameterType", 
     "time": "2015-11-26T15:51:33.932Z" 
    }, 
    { 
     "message": "Unexpected key '0' found in params.Item['pid']", 
     "code": "UnexpectedParameter", 
     "time": "2015-11-26T15:51:33.933Z" 
    }, 
    { 
     "message": "Unexpected key '1' found in params.Item['pid']", 
     "code": "UnexpectedParameter", 
     "time": "2015-11-26T15:51:33.933Z" 
    }, 
    { 
     "message": "Unexpected key '2' found in params.Item['pid']", 
     "code": "UnexpectedParameter", 
     "time": "2015-11-26T15:51:33.933Z" 
    }, 
    { 
     "message": "Unexpected key '3' found in params.Item['pid']", 
     "code": "UnexpectedParameter", 
     "time": "2015-11-26T15:51:33.933Z" 
    }, 
    { 
     "message": "Unexpected key '4' found in params.Item['pid']", 
     "code": "UnexpectedParameter", 
     "time": "2015-11-26T15:51:33.934Z" 
    }, 
    { 
     "message": "Unexpected key '5' found in params.Item['pid']", 
     "code": "UnexpectedParameter", 
     "time": "2015-11-26T15:51:33.934Z" 
    } 
    ], 
    "time": "2015-11-26T15:51:33.944Z" 
} 

मुझे समझ नहीं आता क्यों या यह कैसे हो रही है कुंजी 0, 1, 2, 3, 4 और 5 जब वे पिछली पंक्ति में मुद्रित होने पर मौजूद नहीं हैं।

इसके अलावा, मैं Expected params.Item['pid'] to be a structure त्रुटि को कैसे ठीक करूं? मैंने इसे एक स्ट्रिंग के रूप में घोषित कर दिया है और एक स्ट्रिंग स्टोर करने की कोशिश कर रहा हूं!

अन्य नोट्स: फ़ंक्शन में मैंने जो भी कोड उपयोग किया है, वह ठीक है जब मैं इसे खोल पर चलाता हूं। मैं भी एडब्ल्यूएस-sdk को शामिल किया है और कॉन्फ़िगर कर लेने के रूप में आवश्यक:

var AWS = require('aws-sdk'); 
AWS.config.region = 'us-east-1'; 
AWS.config.endpoint = 'http://localhost:8000/' 
var dynamodb = new AWS.DynamoDB(); 

उत्तर

3

समारोह है कि NodeJS से डेटाबेस में रिकॉर्ड जोड़ने के लिए इस्तेमाल किया जाना चाहिए put और नहीं putItem जो DynamoDB खोल में इस्तेमाल किया जाता है। उपर्युक्त फ़ंक्शन को निम्नलिखित में बदलकर इसे ठीक किया गया है।

function(request, response) { 
    params = { 
    TableName: 'TABLE-NAME', 
    Item: { 
     pid: 'abc123' 
    } 
    }; 
    console.log(params); 
    dynamodb.put(params, function(err, data) { 
    if (err) 
     console.log(JSON.stringify(err, null, 2)); 
    else 
     console.log(JSON.stringify(data, null, 2)); 
    }); 
} 
+3

'put' http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html पर प्रलेखित नहीं किया गया है और जब मैं इसका उपयोग करने का प्रयास करता हूं, तो मुझे एक त्रुटि मिलती है:" TypeError: dynamodb.put है एक समारोह नहीं "। – BlueMonkMN

23

AWS.DynamoDB वर्ग पर putItem() विधि params.Item वस्तु की उम्मीद है एक AttributeValue प्रतिनिधित्व के रूप में स्वरूपित किया जाना है। इसका मतलब है कि अगर आप इसे बदलना होगा: इस में

params = { 
    TableName: 'TABLE-NAME', 
    Item: { 
    pid: 'abc123' 
    } 
}; 

:

params = { 
    TableName: 'TABLE-NAME', 
    Item: { 
    pid: { 
     S: 'abc123' 
    } 
    } 
}; 

आप देशी javascript ऑब्जेक्ट का उपयोग करने के AWS.DynamoDB.DocumentClient वर्ग का उपयोग करना चाहिए चाहते हैं, जो अपने आप पर जावास्क्रिप्ट प्रकार मार्शलों डायनेमो डीबी विशेषता इस तरह:

  • स्ट्रिंग -> एस
  • संख्या -> एन
  • बूलियन -> BOOL
  • अशक्त -> शून्य
  • सरणी -> एल
  • वस्तु -> एम
  • बफर, फ़ाइल, ब्लॉब, ArrayBuffer, DataView, और जावास्क्रिप्ट टाइप सरणी -> बी

यह put() विधि प्रदान करता है, जो AWS.DynamoDB.putItem() पर प्रतिनिधि प्रदान करता है।

संबंधित मुद्दे