Async/await无法在Heroku上运行,但可以在本地运行

qxgroojn  于 2022-11-13  发布在  其他
关注(0)|答案(1)|浏览(158)

我遇到了一个post请求的问题,当我在本地运行我的服务器时,它工作正常,但当我部署到Heroku时,它不工作。这个问题与async/await和promises有关。在本地,当我使用async/await时,我得到一个实际值,当我删除async/await时,我得到一个承诺。这是我所期望的。然而,在我的Heroku应用程序中,无论我做什么,我都得到一个承诺。我重构了很多次,但都没有成功。下面是我代码的一个基本版本:

router.post("/", async (req, res) => {
  const user = "user"
  const org = "org"
  try {
    const newUser = await addUser(user)
    const newOrg = await addOrg(org)
    const newOrgUser = await addOrgUser(newUser, newOrg)
    res.status(201).json({message: "Success"})
  } catch(err) {
    res.status(500).json({message: "Fail"})
  }
});

const addUser = async user => {
  return await db("users").insert(user);
};

const addOrg = async org => {
  return await db("orgs").insert(org);
};

const addOrgUser = async (userId, orgId) => {
  const orgUser = {
    user_id: userId[0],
    org_id: orgId[0]
  }
  return await db("org_users").insert(orgUser)
}

我的问题是newUser和newOrg在尝试addOrgUser时仍然是承诺的,所以userId[0]和orgId[0]是未定义的。这只发生在我的Heroku应用程序上。在本地,我得到了实际的Id。感觉很像await在本地工作,但在Heroku上没有。我错过了什么?谢谢你的帮助。
编辑:package.json:

{
  "scripts": {
    "server": "nodemon index.js",
    "start": "node index.js"
  },
  "dependencies": {
    "@sendgrid/mail": "^7.4.2",
    "bcryptjs": "^2.4.3",
    "cors": "^2.8.5",
    "cross-env": "^7.0.2",
    "dotenv": "^8.2.0",
    "express": "^4.17.1",
    "helmet": "^4.1.1",
    "heroku": "^7.47.11",
    "jsonwebtoken": "^8.5.1",
    "knex": "^0.21.6",
    "knex-cleaner": "^1.3.1",
    "nodemon": "^2.0.6",
    "pg": "^8.5.1",
    "sqlite3": "^5.0.0"
  }
}

Heroku构建日志:

-----> Building on the Heroku-18 stack
-----> Node.js app detected
       
-----> Creating runtime environment
       
       NPM_CONFIG_LOGLEVEL=error
       USE_YARN_CACHE=true
       NODE_ENV=production
       NODE_MODULES_CACHE=true
       NODE_VERBOSE=false
       
-----> Installing binaries
       engines.node (package.json):  unspecified
       engines.npm (package.json):   unspecified (use default)
       engines.yarn (package.json):  unspecified (use default)
       
       Resolving node version 12.x...
       Downloading and installing node 12.20.1...
       Using default npm version: 6.14.10
       Resolving yarn version 1.22.x...
       Downloading and installing yarn (1.22.10)
       Installed yarn 1.22.10
       
-----> Restoring cache
       - yarn cache
       
-----> Installing dependencies
       Installing node modules (yarn.lock)
       yarn install v1.22.10
       warning package.json: No license field
       warning No license field
       [1/4] Resolving packages...
       [2/4] Fetching packages...
       info fsevents@2.1.3: The platform "linux" is incompatible with this module.
       info "fsevents@2.1.3" is an optional dependency and failed compatibility check. Excluding it from installation.
       [3/4] Linking dependencies...
       [4/4] Building fresh packages...
       Done in 13.24s.
       
-----> Build
       
-----> Pruning devDependencies
       yarn install v1.22.10
       warning package.json: No license field
       warning No license field
       [1/4] Resolving packages...
       [2/4] Fetching packages...
       info fsevents@2.1.3: The platform "linux" is incompatible with this module.
       info "fsevents@2.1.3" is an optional dependency and failed compatibility check. Excluding it from installation.
       [3/4] Linking dependencies...
       [4/4] Building fresh packages...
       warning Ignored scripts due to flag.
       Done in 5.73s.
       
-----> Caching build
       - yarn cache
       
-----> Build succeeded!
-----> Discovering process types
       Procfile declares types     -> (none)
       Default types for buildpack -> web
-----> Compressing...
       Done: 43.3M
-----> Launching...
       Released v44
       deployed to Heroku

编辑二:
来自addUser和addOrg响应是相同的:

2021-02-03T02:47:37.624177+00:00 app[web.1]: Result {
2021-02-03T02:47:37.624192+00:00 app[web.1]:   command: 'INSERT',
2021-02-03T02:47:37.624193+00:00 app[web.1]:   rowCount: 1,
2021-02-03T02:47:37.624194+00:00 app[web.1]:   oid: 0,
2021-02-03T02:47:37.624194+00:00 app[web.1]:   rows: [],
2021-02-03T02:47:37.624195+00:00 app[web.1]:   fields: [],
2021-02-03T02:47:37.624196+00:00 app[web.1]:   _parsers: undefined,
2021-02-03T02:47:37.624224+00:00 app[web.1]:   _types: TypeOverrides {
2021-02-03T02:47:37.624225+00:00 app[web.1]:     _types: {
2021-02-03T02:47:37.624226+00:00 app[web.1]:       getTypeParser: [Function: getTypeParser],
2021-02-03T02:47:37.624226+00:00 app[web.1]:       setTypeParser: [Function: setTypeParser],
2021-02-03T02:47:37.624227+00:00 app[web.1]:       arrayParser: [Object],
2021-02-03T02:47:37.624227+00:00 app[web.1]:       builtins: [Object]
2021-02-03T02:47:37.624227+00:00 app[web.1]:     },
2021-02-03T02:47:37.624227+00:00 app[web.1]:     text: {},
2021-02-03T02:47:37.624227+00:00 app[web.1]:     binary: {}
2021-02-03T02:47:37.624228+00:00 app[web.1]:   },
2021-02-03T02:47:37.624228+00:00 app[web.1]:   RowCtor: null,
2021-02-03T02:47:37.624229+00:00 app[web.1]:   rowAsArray: false
2021-02-03T02:47:37.624229+00:00 app[web.1]: } Result {
2021-02-03T02:47:37.624229+00:00 app[web.1]:   command: 'INSERT',
2021-02-03T02:47:37.624229+00:00 app[web.1]:   rowCount: 1,
2021-02-03T02:47:37.624230+00:00 app[web.1]:   oid: 0,
2021-02-03T02:47:37.624230+00:00 app[web.1]:   rows: [],
2021-02-03T02:47:37.624230+00:00 app[web.1]:   fields: [],
2021-02-03T02:47:37.624230+00:00 app[web.1]:   _parsers: undefined,
2021-02-03T02:47:37.624231+00:00 app[web.1]:   _types: TypeOverrides {
2021-02-03T02:47:37.624231+00:00 app[web.1]:     _types: {
2021-02-03T02:47:37.624231+00:00 app[web.1]:       getTypeParser: [Function: getTypeParser],
2021-02-03T02:47:37.624231+00:00 app[web.1]:       setTypeParser: [Function: setTypeParser],
2021-02-03T02:47:37.624231+00:00 app[web.1]:       arrayParser: [Object],
2021-02-03T02:47:37.624232+00:00 app[web.1]:       builtins: [Object]
2021-02-03T02:47:37.624232+00:00 app[web.1]:     },
2021-02-03T02:47:37.624232+00:00 app[web.1]:     text: {},
2021-02-03T02:47:37.624232+00:00 app[web.1]:     binary: {}
2021-02-03T02:47:37.624232+00:00 app[web.1]:   },
2021-02-03T02:47:37.624233+00:00 app[web.1]:   RowCtor: null,
2021-02-03T02:47:37.624233+00:00 app[web.1]:   rowAsArray: false
2021-02-03T02:47:37.624233+00:00 app[web.1]: }

当我检查数据库时,用户和组织记录被正确插入,但是orgUser记录的user_id和org_id为空值。本地addUser和addOrg返回类似于[4]的内容,其中4是插入记录的id。

zed5wv10

zed5wv101#

我知道这是旧的,但我认为问题出在discord.py的版本上。当你使用pip install discord.py时,它只安装最高版本1.7.3,但由于某些原因,Heroku安装了最新的2.0.0版本,这改变了bot在启动时的运行方式。你可以在requirements.txt中指定你希望Heroku运行discord.py==1.7.3的版本,或升级您正在本地运行的版本。如果选择后者,您必须将代码从以下内容更改为:

client.run(token)

变成这样:

async def main():
    async with client(): #or bot depending how you specified it
        await client.start(TOKEN)

相关问题