Javascript 我想添加一个profilePicture并将其保存在NEDB上

Javascript 我想添加一个profilePicture并将其保存在NEDB上,javascript,html,Javascript,Html,我有这个代码的权利知道,但不能保存图片。当我注销并再次登录时,URL会发生更改。 我可以用这段代码做些什么吗?还是我可以用另一段代码? 我听说我可以转换成Base64字符串,但我不知道我是怎么做的,也不知道在哪里 URL为:blob: 你能发布你的服务器代码吗?为什么URL会改变?你在哪里登录和注销?我已经更新了代码。我认为这是因为它保存在de窗口上,当我更改窗口的URL blob时,我更改了URL blob,你能发布你的服务器代码吗?为什么URL会改变?你在哪里登录和注销?我已经更新了代码。我

我有这个代码的权利知道,但不能保存图片。当我注销并再次登录时,URL会发生更改。 我可以用这段代码做些什么吗?还是我可以用另一段代码? 我听说我可以转换成Base64字符串,但我不知道我是怎么做的,也不知道在哪里

URL为:blob:


你能发布你的服务器代码吗?为什么URL会改变?你在哪里登录和注销?我已经更新了代码。我认为这是因为它保存在de窗口上,当我更改窗口的URL blob时,我更改了URL blob,你能发布你的服务器代码吗?为什么URL会改变?你在哪里登录和注销?我已经更新了代码。我认为这是因为它保存在de窗口上,当我更改窗口时,我更改了URL blob
window.addEventListener('load', function() {
    document.querySelector('input[type="file"]').addEventListener('change', function() {
        if (this.files && this.files[0]) {
            var img = document.querySelector(".myImg"); // $('img')[0]
            img.onload = imageIsLoaded;
            img.src = URL.createObjectURL(this.files[0]); // set src to blob url
        }
    });
});

});
  <div class="Profil__Picture-Button">
     <img src="./img/LogoFinderV21.png" alt="your image" class="Profile__Profile-Picture myImg">
     <input type="file" />
  </div>
app.post('/login', async(req, res) => {
    user = await collectionsNEDB.users.find({})
    let matchedUser
    for (let i = 0; i < user.length; i++) {
        console.log(user[i].username)
        console.log(user[i]._id)
        if (req.body.username == user[i].username && req.body.password == user[i].password) {
            matchedUser = user[i]
            break
        }
    }
    if (matchedUser) {
        const payload = { userId: matchedUser._id }
        const token = jwt.sign(payload, "hej", { expiresIn: '1s' })
        res.json({ token, userId: matchedUser._id, user })
    } else {
        res.status(403).json({ error: 'Invalid Credentials' })
    }
})

app.get('/secured', auth, (req, res) => {
    res.json({ message: `${req.user}` })
})

async function updateUser(age, city, gender, games, discord, steam, origin, image) {
    const id = localStorage.getItem("userId")
    const response = await fetch('http://localhost:8080/users/' + id, {
        method: 'PATCH',
        headers: {
            'Content-Type': 'application/json',
        },
        body: JSON.stringify({
            age: age,
            city: city,
            gender: gender,
            games: games,
            usernameDiscord: discord,
            usernameSteam: steam,
            usernameOrigin: origin,
            image: image
        })
    })
    console.log(response)
    const data = await response.json()
    console.log(data)
}

function updateUsersIndex() {
    let updateProfileBtn = document.querySelector(".Profile-Right__Update")
    console.log(updateProfileBtn)
    updateProfileBtn.addEventListener("click", async(event) => {
        console.log("hej")
        event.preventDefault()
        const age = document.querySelector(".Age__Input").value
        const city = document.querySelector(".City__Input").value
        const gender = document.querySelector(".Gender__Input").value
        const discord = document.querySelector(".Discord__Input").value
        const steam = document.querySelector(".Steam__Input").value
        const origin = document.querySelector(".Origin__Input").value
        const games = document.querySelector(".Profile-Right__Select-Game").value
        const image = document.querySelector(".Profile__Profile-Picture").src
        const hidden = document.querySelector(".hidden")
        const updateUsers = await updateUser(age, city, gender, games, discord, steam, origin, image)
        window.location.reload(true)
        toggling([".Profile__Wrappe"])
    })
}
updateUsersIndex()