i'm junior fronted developer, and i want to your help...
below my pages code
const sendAnotherIp = async () => {
const domain_format = /^(((http(s?))\:\/\/)?)([0-9a-zA-Z\-] \.) [a-zA-Z]{2,6}(\:[0-9] )?(\/\S*)?/;
if (domain_format.test(anotherIp)) {
await axios
.get(`${process.env.NEXT_PUBLIC_CLIENT_URL}/api/dnslookup`, {
params: {
search: anotherIp,
date: nowTime,
},
})
.then((res) => {
alert(res.data);
});
} else {
alert("it's wrong domain format");
}
};
console.log(JSON.stringify(data)); -> "[Object object]"
below my api code
import { NextApiRequest, NextApiResponse } from "next";
const sqlite3 = require("sqlite3").verbose();
const dns = require("dns");
interface Iquerys {
search: string;
date: string;
}
interface IUrl {
ip: string;
}
const dnslookup = (request: NextApiRequest, response) => {
const req = response.req;
const querys: Iquerys = req.query;
const searchData = querys.search;
const dateData = querys.date;
// below my logic
response.status(200).end(
`${dns.resolveAny(searchData, function (err: any, addresses: any) {
return addresses;
})}`
);
//
// db connect
let db = new sqlite3.Database("./db/my_database.db", (err) => {
if (err) {
return console.log(err.message);
}
console.log("Connected to database!");
});
// db peristalsis
db.run("INSERT INTO DnsLookup(search, date) VALUES (?, ?)", [searchData, dateData], (err) => {
if (err) {
return console.log(err.message);
}
console.log("Row was added to the table: ${this.lastID}");
});
};
export default dnslookup;
my project build in react, next js. I used to JSON.parse() and JSON.stringify() and toString() ... etc but I can't find a way to real data. what's wrong with my code? help me please guys
CodePudding user response:
I think the server code is the problem. You should wait for the call back to complete then you should send the response.
response.status(200).end(
`${dns.resolveAny(searchData, function (err: any, addresses: any) {
return addresses;
})}`
);
On the front end code, you should remove await in front of Axios that is not needed.
CodePudding user response:
First declare the headers to be Application/Json
axios.get('m', { headers: { 'Content-Type': 'application/json' } })