【问题标题】:Saving aws S3 image location links to postgres db table将 aws S3 图像位置链接保存到 postgres 数据库表
【发布时间】:2021-10-16 18:34:33
【问题描述】:

我有一个通过 Heroku 的 postgres 数据库,带有一个 PUT 路由来保存上传的 s3 存储桶图像链接到数据库,但是链接没有保存到数据库表中。没有错误,我收到了,但链接根本没有保存到我正在调用数据库的更新查询的表中。谁能说这里有什么问题?

//Here is the table scheme


       CREATE TABLE Users_Channel(
        id SERIAL PRIMARY KEY UNIQUE,
        userID INT UNIQUE,
        FOREIGN KEY(userID) REFERENCES Users(id),
        
        channelName varchar(255) UNIQUE,
        FOREIGN KEY(channelName) REFERENCES Users(Username),
 
        Profile_Avatar TEXT NULL,
        Slider_Pic1 TEXT NULL,
        Slider_Pic2 TEXT NULL,
        Slider_Pic3 TEXT NULL,
        Subscriber_Count INT NULL,
        UNIQUE(channelName, userID)
      );


//Database Update Query to updated channel by channel name

async function updateChannel({
  channelname,
  profile_avatar,
  slider_pic1,
  slider_pic2,
  slider_pic3
}) {
  try {
    const { rows } = await client.query(
      `
              UPDATE users_channel
              SET  profile_avatar=$2, slider_pic1=$3, slider_pic2=$4, slider_pic3=$5
              WHERE channelname=$1
              RETURNING *;
            `,
      [channelname, profile_avatar, slider_pic1, slider_pic2, slider_pic3]
    );
    return rows;
  } catch (error) {
    throw error;
  }
}


//API Put Route

usersRouter.put(
  "/myprofile/update/:channelname",
  profileUpdate,
  requireUser,
  async (req, res, next) => {
    const { channelname } = req.params;
    
    
      const pic1 = req.files["avatar"][0];
      const pic2 = req.files["slide1"][0];
      const pic3 = req.files["slide2"][0];
      const pic4 = req.files["slide3"][0];
    
    try {
      const result = await uploadFile(pic1);
      const result1 = await uploadFile(pic2);
      const result2 = await uploadFile(pic3);
      const result3 = await uploadFile(pic4);

      console.log(result, result1, result2, result3);

      const updateData = {
        profile_avatar: result.Location,
        slider_pic1: result1.Location,
        slider_pic2: result2.Location,
        slider_pic3: result3.Location,
      };

      console.log(updateData);
      const updatedchannel = await updateChannel(channelname, updateData);
      res.send({ channel: updatedchannel });
    } catch (error) {
      console.error("Could not update user profile", error);
      next(error);
    }
  }
);

【问题讨论】:

    标签: postgresql api amazon-s3 heroku


    【解决方案1】:

    解决了它必须将我的更新查询部分修改为以下内容。取出大括号并用大括号创建一个变量并将其传递给函数。

    async function updateChannel(channelname, photos) {
      const { profile_avatar, slider_pic1, slider_pic2, slider_pic3} = photos;
      try {
        const { rows } = await client.query(
          `
                  UPDATE users_channel
                  SET profile_avatar=$2, slider_pic1=$3, slider_pic2=$4, slider_pic3=$5
                  WHERE channelname=$1
                  RETURNING *;
                `,
          [channelname, profile_avatar, slider_pic1, slider_pic2, slider_pic3]
        );
        return rows;
      } catch (error) {
        throw error;
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-08
      • 1970-01-01
      • 2019-01-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多