部署到 Heroku 时出现“错误:ENOENT:没有这样的文件或目录”

问题描述

我正在尝试将我的应用程序部署到 Heroku,当我进入某个页面时,我的应用程序出现内部服务器错误。这太奇怪了;有时它只在我部署时有效,有时它会给我这个错误

代码

const storage = require('../modules/storage')

module.exports = (req,res) => {

// Unique user code
givenCode = req.body.uniqueCode

// new empty object for the given code.
const newObj = {
    code: givenCode,personal: {
        name: "",surname: "",age: "",gender: "",favoriteGame: "",},"game_personal": {
        favoritePlatform: "",averageGameTime: "",prefer: "",favoriteGameGenre: "",gamertag: "","open_questions": {
        favoriteGameOfAllTime: "",whatWouldYouDo: "",intoVideoGames: "","rate_game": {
        opinionAboutTheGame: "",rate: "",timeSpend: "",recommend: "",}

// takes the object and adds it to a json file in the storage map with the unique code as name
storage.saveNewData(newObj,`./storage/${givenCode}.json`)

res.render('personal',{ 
    uniqueCode: givenCode,})

这一行出错了:

storage.saveNewData(newObj,`./storage/${givenCode}.json`)

存储代码

const fs = require('fs')

module.exports = {
    // Checks if the file exists and gives back an boolean
    checksIfFileExists: path => fs.existsSync(path),// Gets the data if it exists and parse it to an object. the readFileSync 
    // reads the contents of the path synchronously with an extra option for utf-8
    getExistingData: path => JSON.parse(fs.readFileSync(path,{
        encoding: 'utf8',flag:'r',})),// Will save the new data to the path when it is submitted. Also turns the data
    // it into a JSON string and after that returns it. The extra parameters give a good
    // formatted JSON file
    saveNewData: (data,path) => {
        fs.writeFileSync(path,JSON.stringify(data,null,2))
        return data
    }
}

错误

2021-03-30T19:11:31.912533+00:00 app[web.1]: Error: ENOENT: no such file or directory,open './storage/b0abd8a8.json'
2021-03-30T19:11:31.912538+00:00 app[web.1]: at Object.openSync (fs.js:476:3)
2021-03-30T19:11:31.912539+00:00 app[web.1]: at Object.writeFileSync (fs.js:1467:35)
2021-03-30T19:11:31.912539+00:00 app[web.1]: at Object.saveNewData (/app/modules/storage.js:16:12)
2021-03-30T19:11:31.912540+00:00 app[web.1]: at module.exports (/app/routes/personal.js:39:13)
2021-03-30T19:11:31.912540+00:00 app[web.1]: at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)
2021-03-30T19:11:31.912541+00:00 app[web.1]: at next (/app/node_modules/express/lib/router/route.js:137:13)
2021-03-30T19:11:31.912541+00:00 app[web.1]: at Route.dispatch (/app/node_modules/express/lib/router/route.js:112:3)
2021-03-30T19:11:31.912542+00:00 app[web.1]: at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)
2021-03-30T19:11:31.912542+00:00 app[web.1]: at /app/node_modules/express/lib/router/index.js:281:22
2021-03-30T19:11:31.912543+00:00 app[web.1]: at Function.process_params (/app/node_modules/express/lib/router/index.js:335:12)

我是一名初级开发者。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)