You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Heroku部署应用显示MongoDB已连接但无法更新数据库

问题现象

本地环境下开发的两个应用所有MongoDB操作均运行正常,部署到Heroku线上环境后,日志显示MongoDB已成功连接,但始终无法执行数据库更新操作。
已确认Heroku配置中正确添加了MONGODB_URL配置变量——如果该配置错误,应用不会返回数据库连接成功的状态。故障接口的示例代码如下:

app.get('/update/:entry', async (req, res) => {
    let entryArray = []
    const basicInfo = await axios.get(`https://api.opensea.io/api/v1/collection/${req.params.entry}`)
    const name = basicInfo.data.collection.name.toString()
    const slug = basicInfo.data.collection.slug.toString()
    const symbol = basicInfo.data.collection.primary_asset_contracts[0]?.symbol?.toString() || ''
    const description = basicInfo.data.collection.description?.toString() || ''
    const verifiedStatus = basicInfo.data.collection.safelistRequestStatus?.toString() || ''
    const bannerUrl = basicInfo.data.collection.banner_image_url?.toString() || ''
    const logoUrl = basicInfo.data.collection.image_url?.toString() || ''
    const stats = {
        one_day_volume: basicInfo.data.collection.stats.one_day_volume,
        one_day_change: basicInfo.data.collection.stats.one_day_change,
        one_day_sales: basicInfo.data.collection.stats.one_day_sales,
        one_day_average_price: basicInfo.data.collection.stats.one_day_average_price,
        seven_day_volume: basicInfo.data.collection.stats.seven_day_volume,
        seven_day_change: basicInfo.data.collection.stats.seven_day_change,
        seven_day_sales: basicInfo.data.collection.stats.seven_day_sales,
        seven_day_average_price: basicInfo.data.collection.stats.seven_day_average_price,
        thirty_day_volume: basicInfo.data.collection.stats.thirty_day_volume,
        thirty_day_change: basicInfo.data.collection.stats.thirty_day_change,
        thirty_day_sales: basicInfo.data.collection.stats.thirty_day_sales,
        thirty_day_average_price: basicInfo.data.collection.stats.thirty_day_average_price,
        total_volume: basicInfo.data.collection.stats.total_volume,
        total_sales: basicInfo.data.collection.stats.total_sales,
        total_supply: basicInfo.data.collection.stats.total_supply,
        count: basicInfo.data.collection.stats.count,
        average_price: basicInfo.data.collection.stats.average_price,
        num_reports: basicInfo.data.collection.stats.num_reports,
        market_cap: basicInfo.data.collection.stats.market_cap,
    }
    const social = {
        discord: basicInfo.data.collection.discord_url?.toString() || '',
        medium: basicInfo.data.collection.medium_username?.toString() || '',
        twitter: basicInfo.data.collection.twitter_username?.toString() || '',
        instagram: basicInfo.data.collection.instagram_username?.toString() || '',
        wiki: basicInfo.data.collection.wiki_url?.toString() || ''
    }

    let floorPrice = 0
    if (basicInfo.data.collection.stats.floor_price == null) {
        floorPrice = 0
    } else {
        floorPrice = basicInfo.data.collection.stats.floor_price
    }

    entryArray.push({"name":name, "slug":slug, "symbol":symbol, "description":description, "isVerified":verifiedStatus, "banner":bannerUrl, "logo":logoUrl, "floorPrice":floorPrice, "stats":stats, "socials":social})

    NFT.findOne({ "name": name }, async (err, value) => {
        if (err) { console.error(err) }
        if (!value) {
            const newNFT = new NFT({"name":name, "slug":slug, "symbol":symbol, "description":description, "isVerified":verifiedStatus, "banner":bannerUrl, "logo":logoUrl, "floorPrice":floorPrice, "stats":stats, "socials":social})
            await newNFT.save()
        } 
        else {
                // 更新已存在的NFT记录
                value.name = name 
                value.slug = slug
                value.symbol = symbol
                value.description = description
                value.verifiedStatus = verifiedStatus
                value.bannerUrl = bannerUrl
                value.logoUrl = logoUrl
                value.floorPrice = floorPrice
                value.stats = stats
                value.social = social
                await value.save();
        }
    })
    res.json(entryArray)
})
排查进展
  • 若在Heroku配置MongoDB连接参数时,给配置值额外包裹双引号,会触发URI不符合开头格式要求的报错
  • 修复上述配置问题后,应用运行无崩溃、无报错日志,但数据库更新操作依然无法正常执行

内容的提问来源于stack exchange,提问作者Daniel Brannon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.29 13:06:31