Async/await 在 Express.js POST 处理程序中
Async/await in Express.js POST handler
我正在尝试在 POST 处理程序中调用异步函数。
我尝试调用的函数如下(此代码有效):
const seaport = require("./seaport.js");
// This function creates a fixed price sell order (FPSO)
async function createFPSO(ownerAddress, contractAddress, tokenId, startAmount, expirationTime = 0) {
const fixedPriceSellOrder = await seaport.createSellOrder({
asset: {
tokenId: tokenId,
tokenAddress: contractAddress,
},
startAmount: startAmount,
expirationTime: expirationTime,
accountAddress: ownerAddress,
});
console.log(`Successfully created a fixed-price sell order! ${fixedPriceSellOrder.asset.openseaLink}\n`);
}
createFPSO(MY_ADDRESS, NFT_CONTRACT_ADDRESS, "2", 0.01);
由于上面的代码有效,我现在尝试将其传输到 POST 处理程序,如下所示:
const express = require('express');
const app = express();
app.use(express.json());
const seaport = require("./seaport.js");
app.post('/create/fixedprice', async (req, res, next)=> {
try {
const fixedPriceSellOrder = await seaport.createSellOrder({
asset: {
tokenId: req.body.tokenId,
tokenAddress: req.body.contractAddress,
},
startAmount: req.body.startAmount,
expirationTime: req.body.expirationTime,
accountAddress: req.body.ownerAddress,
});
console.log(`Successfully created a fixed-price sell order! ${fixedPriceSellOrder.asset.openseaLink}\n`);
res.send(fixedPriceSellOrder);
} catch (error) {
return next(error);
}
});
const port = process.env.PORT || 13579;
app.listen(port, () => console.log(`Service is listening on port ${port}..`));
但是,这会导致错误(我怀疑错误消息是否相关,但仍然分享它):
Error: No wallet address found
at Object.validateAndFormatWalletAddress (/root/opensea/opensea-creatures/node_modules/opensea-js/src/utils/utils.ts:928:11)
at OpenSeaPort.<anonymous> (/root/opensea/opensea-creatures/node_modules/opensea-js/src/seaport.ts:1994:22)
at step (/root/opensea/opensea-creatures/node_modules/opensea-js/lib/seaport.js:40:23)
at Object.next (/root/opensea/opensea-creatures/node_modules/opensea-js/lib/seaport.js:21:53)
at /root/opensea/opensea-creatures/node_modules/opensea-js/lib/seaport.js:15:71
at new Promise (<anonymous>)
at __awaiter (/root/opensea/opensea-creatures/node_modules/opensea-js/lib/seaport.js:11:12)
at OpenSeaPort._makeSellOrder (/root/opensea/opensea-creatures/node_modules/opensea-js/lib/seaport.js:2038:16)
at OpenSeaPort.<anonymous> (/root/opensea/opensea-creatures/node_modules/opensea-js/src/seaport.ts:625:30)
at step (/root/opensea/opensea-creatures/node_modules/opensea-js/lib/seaport.js:40:23)
at Object.next (/root/opensea/opensea-creatures/node_modules/opensea-js/lib/seaport.js:21:53)
at /root/opensea/opensea-creatures/node_modules/opensea-js/lib/seaport.js:15:71
at new Promise (<anonymous>)
at __awaiter (/root/opensea/opensea-creatures/node_modules/opensea-js/lib/seaport.js:11:12)
at OpenSeaPort.createSellOrder (/root/opensea/opensea-creatures/node_modules/opensea-js/lib/seaport.js:604:16)
at /root/opensea/opensea-creatures/scripts/service.js:58:47
我要说的是,当我将第一个列表中的相同功能传输到 POST 处理程序时,它现在会崩溃,所以我怀疑它与 OpenSea SDK 有什么关系被打破了。
错误信息非常相关。错误消息是 Error: No wallet address found
。这意味着处理钱包地址的任何参数,很可能 accountAddress
设置为 req.body.ownerAddress
,为空或无效。
我会仔细检查您请求正文中的值,因为它们是可能的原因。
我正在尝试在 POST 处理程序中调用异步函数。
我尝试调用的函数如下(此代码有效):
const seaport = require("./seaport.js");
// This function creates a fixed price sell order (FPSO)
async function createFPSO(ownerAddress, contractAddress, tokenId, startAmount, expirationTime = 0) {
const fixedPriceSellOrder = await seaport.createSellOrder({
asset: {
tokenId: tokenId,
tokenAddress: contractAddress,
},
startAmount: startAmount,
expirationTime: expirationTime,
accountAddress: ownerAddress,
});
console.log(`Successfully created a fixed-price sell order! ${fixedPriceSellOrder.asset.openseaLink}\n`);
}
createFPSO(MY_ADDRESS, NFT_CONTRACT_ADDRESS, "2", 0.01);
由于上面的代码有效,我现在尝试将其传输到 POST 处理程序,如下所示:
const express = require('express');
const app = express();
app.use(express.json());
const seaport = require("./seaport.js");
app.post('/create/fixedprice', async (req, res, next)=> {
try {
const fixedPriceSellOrder = await seaport.createSellOrder({
asset: {
tokenId: req.body.tokenId,
tokenAddress: req.body.contractAddress,
},
startAmount: req.body.startAmount,
expirationTime: req.body.expirationTime,
accountAddress: req.body.ownerAddress,
});
console.log(`Successfully created a fixed-price sell order! ${fixedPriceSellOrder.asset.openseaLink}\n`);
res.send(fixedPriceSellOrder);
} catch (error) {
return next(error);
}
});
const port = process.env.PORT || 13579;
app.listen(port, () => console.log(`Service is listening on port ${port}..`));
但是,这会导致错误(我怀疑错误消息是否相关,但仍然分享它):
Error: No wallet address found
at Object.validateAndFormatWalletAddress (/root/opensea/opensea-creatures/node_modules/opensea-js/src/utils/utils.ts:928:11)
at OpenSeaPort.<anonymous> (/root/opensea/opensea-creatures/node_modules/opensea-js/src/seaport.ts:1994:22)
at step (/root/opensea/opensea-creatures/node_modules/opensea-js/lib/seaport.js:40:23)
at Object.next (/root/opensea/opensea-creatures/node_modules/opensea-js/lib/seaport.js:21:53)
at /root/opensea/opensea-creatures/node_modules/opensea-js/lib/seaport.js:15:71
at new Promise (<anonymous>)
at __awaiter (/root/opensea/opensea-creatures/node_modules/opensea-js/lib/seaport.js:11:12)
at OpenSeaPort._makeSellOrder (/root/opensea/opensea-creatures/node_modules/opensea-js/lib/seaport.js:2038:16)
at OpenSeaPort.<anonymous> (/root/opensea/opensea-creatures/node_modules/opensea-js/src/seaport.ts:625:30)
at step (/root/opensea/opensea-creatures/node_modules/opensea-js/lib/seaport.js:40:23)
at Object.next (/root/opensea/opensea-creatures/node_modules/opensea-js/lib/seaport.js:21:53)
at /root/opensea/opensea-creatures/node_modules/opensea-js/lib/seaport.js:15:71
at new Promise (<anonymous>)
at __awaiter (/root/opensea/opensea-creatures/node_modules/opensea-js/lib/seaport.js:11:12)
at OpenSeaPort.createSellOrder (/root/opensea/opensea-creatures/node_modules/opensea-js/lib/seaport.js:604:16)
at /root/opensea/opensea-creatures/scripts/service.js:58:47
我要说的是,当我将第一个列表中的相同功能传输到 POST 处理程序时,它现在会崩溃,所以我怀疑它与 OpenSea SDK 有什么关系被打破了。
错误信息非常相关。错误消息是 Error: No wallet address found
。这意味着处理钱包地址的任何参数,很可能 accountAddress
设置为 req.body.ownerAddress
,为空或无效。
我会仔细检查您请求正文中的值,因为它们是可能的原因。