我正在尝试将我的express路由器函数连接到ReactJS。目前我的代码看起来像这样:
nftmodule.js
import {ZDK} from '@zoralabs/zdk'
const zdk = new ZDK("https://api.zora.co/graphql")
export async function fetchTokens(zdk, collectionAddresses){
return await zdk.tokens({
where: {
collectionAddresses
}
})
}
const tokens = await fetchTokens(zdk, '0x42069ABFE407C60cf4ae4112bEDEaD391dBa1cdB')
export function aToken(){
return tokens
}
然后它突然出现在这里
nftwholecollection.js
import { aToken, fetchTokens} from './nftmodule.js'
import {ZDK} from '@zoralabs/zdk'
const zdk = new ZDK("https://api.zora.co/graphql")
let token = await fetchTokens(zdk, '0x42069ABFE407C60cf4ae4112bEDEaD391dBa1cdB')
let thistoken = JSON.stringify(token,null,3)
console.log(JSON.parse(thistoken).tokens.nodes[3].token.image)
let collectionSize = 40;
const tokens = aToken()
let nftGallery = JSON.stringify(tokens,null,3)
let x
export function loopLinks()
{
let output =""
for(x =0;x<collectionSize; x++)
if (JSON.parse(nftGallery).tokens.nodes[x].token.image.mediaEncoding.__typename != "UnsupportedEncodingTypes") {
output = output + `<img src=${JSON.stringify(JSON.parse(nftGallery).tokens.nodes[x].token.image.mediaEncoding.thumbnail)} loading='lazy'>`
}
return output
}
console.log(loopLinks())
然后最后nftroutter.js
import {loopLinks} from './nftwholecollection.js'
import express from 'express'
console.log(loopLinks())
const app = express();
const port = 5150;
app.get('/api/nft',(req,res)=>{
res.setHeader("Content-Type", "text/html")
res.write(loopLinks())
res.end()
})
app.listen( port ,()=>{
console.log("the server got 5150'd")
});
看来我有两个选择。我可以想办法让我的模块出现在react.js中,也可以想办法用commonjs编写上面的代码。我正试图做后者。
我遇到了几个问题,基本上都集中在试图找出";let token=wait fetchTokens(zdk,"blablablacrypto"(";
当我试图在packagejson中将类型设置为commonjs时,我显然在nftmodule中遇到了一个错误,即不能在commonjs中的异步函数体之外引用wait。对此,commonjs解决方案是什么?
我会尝试这样的方法来使用顶级等待。
nftmodule.js
import {ZDK} from '@zoralabs/zdk'
const zdk = new ZDK("https://api.zora.co/graphql")
async function fetchTokens(zdk, collectionAddresses){
return await zdk.tokens({
where: {
collectionAddresses
}
})
}
// Here we use top level await
export default await fetchTokens(zdk, '0x42069ABFE407C60cf4ae4112bEDEaD391dBa1cdB')
nftwholecollection.js
// Now this module will wait for the promise to resolve before executing
import tokens from './nftmodule.js'
let collectionSize = 40;
let nftGallery = JSON.stringify(tokens,null,3)
let x
export function loopLinks()
{
let output =""
for(x =0;x<collectionSize; x++)
if (JSON.parse(nftGallery).tokens.nodes[x].token.image.mediaEncoding.__typename != "UnsupportedEncodingTypes") {
output = output + `<img src=${JSON.stringify(JSON.parse(nftGallery).tokens.nodes[x].token.image.mediaEncoding.thumbnail)} loading='lazy'>`
}
return output
}
console.log(loopLinks())
nftrouter.js(与以前相同(
import {loopLinks} from './nftwholecollection.js'
import express from 'express'
console.log(loopLinks())
const app = express();
const port = 5150;
app.get('/api/nft',(req,res)=>{
res.setHeader("Content-Type", "text/html")
res.write(loopLinks())
res.end()
})
app.listen( port ,()=>{
console.log("the server got 5150'd")
});