簡體   English   中英

獲取密鑰的 dynamodb 信息時,我的 promise 未定義

[英]My promise is undefined when getting dynamodb info for a key

每當我返回 promise 時,我都會得到 undefined。

異步 Function 從 ddb 檢索項目

async function getTask(){
    const userName = 'jared'; //key name
    const userTable = 'todoUsers'; //table name
async function getTask(){  //get list of tasks for key
   const params = {    //params for dynamodb.get
    Key:{
        "username": userName
    },
    TableName: userTable,
    ConsistentRead: true,
};
 return await dynamodb.update(params).promise().then(response => { //returning response
 return response;
}, error =>{
    console.log('There is an error getting task: ', error);
});

    

}

您提供的代碼似乎不正確,因為您定義了 getTask() 兩次。 我也不明白你為什么await然后使用then

試試這個,看看返回是否被定義:

  public static async getTask() {
    const userName = 'jared'; //key name
    const userTable = 'todoUsers'; //table name
    const params = {
      //params for dynamodb.get
      Key: {
        username: userName
      },
      TableName: userTable,
      ConsistentRead: true
    };
    return documentClient.update(params).promise();
  }

在我用try catch之后它起作用了。

async function getTasks(){
    const userName = 'jared';
    const userTable = 'todoUsers';
    try {
        var params = {
            KeyConditionExpression: 'username = :user',
            ExpressionAttributeValues: {
                ':user': userName
            },
            TableName: userTable
        };
        var result = await dynamodb.query(params).promise()
        return JSON.stringify(result.Items);
    } catch (error) {
        console.error(error);
    }


}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM