簡體   English   中英

express js:如何顯示來自靜態/公共目錄之外的圖像

[英]express js: how to display images from outside of static/public dir

我正在嘗試提供來自不同網址的圖像,例如:firebase 存儲、github、imgbb 等……但它不起作用!

讓我解釋一下...這里我在 mongodb 上存儲了 img url

.....
profile_img: "https://avatars.githubusercontent.com/u/51321911?v=4"
cover_img: "https://pbs.twimg.com/profile_banners/820249925384630273/1600164235/60…"
.....

我已經完美地查詢了數據......只有img沒有顯示!

<img src="{{ userData.profile_img }}" //not work

但本地圖像工作..來自靜態文件夾

<img src="/static/resource/default_avatar.png" //working
//controller.js

const User = require("../../models/User");

const profile = async (req, res) => {
    const otherUsers = await User.find({_id: {$ne: res.locals.user.id}});
    const userData = await User.findOne({username: req.params.username}).select("-password");
    console.log(userData);
    res.render("user/profile.html", {otherUsers, userData});
}

module.exports = {
    profile,
};
userData - output----
{
  _id: new ObjectId("62b702ba4502ee54e96dba64"),
  fname: 'Muhammad',
  surname: 'ABir',
  username: 'muhammadabir',
  email: 'abir@gmail.com',
  profile_img: 'https://avatars.githubusercontent.com/u/51321911?v=4',
  cover_img: 'https://pbs.twimg.com/profile_banners/820249925384630273/1600164235/600x200',
  is_active: true,
  star_badge: true,
  joined: 'Jun 25, 2022',
  friends: [],
  __v: 0
}

你試過不帶引號(“”)嗎?

<img src= {{ userData.profile_img }}
// or 
<img src= { userData.profile_img }

暫無
暫無

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

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