[英]Saving aws S3 image location links to postgres db table
我有一个通过 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);
}
}
);
解决它必须将我的更新查询部分修改为下面的内容。 取出大括号并创建一个带有大括号的变量并将其传递给 function。
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.