const express = require("express");
const app = express();
const axios = require("axios");
const ethers = require("ethers");
require("dotenv").config();
app.get("/:address", async (req, res) => {
const SUBGRAPH = process.env.SubGraph_URL;
const ServerAddress = process.env.Server_ADDRESS;
const Server_ABI = process.env.Server_ABI;
let address = req.params.address;
let query = `{
publishers(
where: {Publisher: "${address}"}
) {
Advertisers
}
}`;
let AdId;
let AdLink;
await axios
.post(SUBGRAPH, { query })
.then((response) => {
return response.data.data.publishers[0].Advertisers.length;
})
.then((length) => {
let x = Math.floor(Math.random() * length);
AdId = x + 1;
})
.catch((error) => {
console.log(error);
});
query = `{
ads(where:{AdId:"${AdId}"}) {
AdData
}
}`;
await axios
.post(SUBGRAPH, { query })
.then((response) => {
let x = response.data.data.ads[0];
return x.AdData;
})
.then((link) => {
let x = axios.get(link);
return x;
})
.then((response) => {
AdLink = response.data.ImgLink;
})
.catch((error) => {
console.log(error);
});
const handleClick = async () => {
};
const html = `
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1>Ad</h1>
<a href="">
<img src="${AdLink}" alt="Ad Image" border="1" onClick="${handleClick}"></img></a>
<img src="xyz/abc/cls
}" height="1" width="1" style={display:none} />
</body>
</html>`;
res.send(html);
res.end();
});
app.listen(3000, () => {
console.log("Server is running on port 3000");
});
im my code after running it on localhost im etting the links and desired html but also getting an error msg on console i tried differnet ways but im getting nothing Im new to this can anoyone explain the error im ggetting this error
TypeError: Cannot read properties of undefined (reading 'publishers')
at C:\Users\rishi\OneDrive\Desktop\Karthikeya\Ad-Server-API\index.js:24:33
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async C:\Users\rishi\OneDrive\Desktop\Karthikeya\Ad-Server-API\index.js:21:3
TypeError: Cannot read properties of undefined (reading 'ads')
at C:\Users\rishi\OneDrive\Desktop\Karthikeya\Ad-Server-API\index.js:42:34
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async C:\Users\rishi\OneDrive\Desktop\Karthikeya\Ad-Server-API\index.js:39:3
in this if an user makes an request to our api with his address we will send an html doc containing the img it is an ad server project