首页 > 解决方案 > API 未更新表字段 - React Native App、AWS API 和 DynamoDB 表

问题描述

尝试使用我的“更新”API 更新我的 DynamoDB 表。它只是不工作。“更新”API 会将“状态”字段从“新建”更新为“已读”。API 没有问题,因为我在 Postman 上检查了它,并且它正确更新了该字段。

export const updateNotifications = (id) => {

       if(id !== null){
        const config = getAppConfig();

        const cognitoToken = await getCognitoToken(config); 

        if (cognitoToken !== null) {
          try{
            let headers = {
              'Content-Type': 'application/json',
              'Authorization': cognitoToken,
              'targetId': HAGo_getDeviceUUID(),
              'userId' : id
            };

            let body = { 'status' : 'Read' }

            let updateUrl = config["notification-update-api"] + '/' + id;

            await fetch(updateUrl,
              {
                method: 'PATCH',
                headers: headers,
                body: body
              })

          } catch (e) {
              // something went wrong
              console.log('error'); 
          }
        }
      }else{
        return null;
      }

    }

我这样做对吗???请帮我修复我的代码!!!!

标签: javascriptreactjsamazon-web-servicesapireact-native

解决方案


解决它

解决方法:将json.stringify 给body 固定好。

export const updateNotifications = (id) => {

.
.
.
.
.

            let body = json.stringify({ 'status' : 'Read' })

            await fetch(updateUrl,
              {
                method: 'PATCH',
                headers: headers,
                body: body
              }

    }


推荐阅读