从回调中存储数据库中的数据
store data from database from call back
我正在做 node js 项目,我正在使用 ipstack 从 IP 获取用户数据。无法理解如何从回调函数中获取数据并将其插入到我的数据库中。
这是我的代码
const ipstack = require('ipstack');
const ip = '103.195.74.60';
ipstack(ip, process.env.IPSTACK, (err, response) => {
console.log(response);
});
在此处插入查询
// create user
await User.create({
fullName,
phone,
email,
nikeName,
ip,
city (come from ip),
zip (come from ip),
browser,
device,
});
您可以只需要在使用 console.log
调用的位置精确地进行数据库牵引,如下所示:
const ipstack = require("ipstack");
const ip = "103.195.74.60";
ipstack(ip, process.env.IPSTACK, (err, response) => {
// First of all you need to check if there is error
if (err){
// handle error hreer, you might need to use `return`
// at some point to not execute the below code, alternatively
// you could just wrap it in `else`
}
console.log(response); // your orginal `console.log` call
//your database transaction ca be done here,
// create user
await User.create({
fullName,
phone,
email,
nikeName,
ip,
response.city // need to look in documentation
response.zip // or explore the response object terminal
browser,
device,
})
});
我访问响应键的代码部分是推测,您只需要查找 ipstack
包的文档(如果存在),或者只需浏览给您的模式 response
是 console.log
.
最后,我建议使用 official documentation given the lipstick
is not really maintained the last update was 3 years ago, add to that by following the official documentation you could use fetch
or axios
and take advantage of async
await
功能,这是当前处理异步代码的现代方法*。
*这可能是约定俗成的观点。
@Ghassan Maslamani 在按照您的建议进行操作后一直运行顺利。检查代码,如果需要对代码进行任何改进,请指导我。再次感谢。
// get user ip
const ip = req.headers['x-forwarded-for'] || req.socket.remoteAddress;
// location details through Api call
const url = `http://api.ipstack.com/${ip}?access_key=${process.env.IPSTACK}`;
async function locationInfo() {
try {
const result = await axios.get(url);
const data = await result.data;
return data;
} catch (error) {
console.log(error);
}
}
const locInfo = await locationInfo();
// City name
const location = `${locInfo.city},${locInfo.country_name}`;
// zip code
const { zip } = locInfo;
我正在做 node js 项目,我正在使用 ipstack 从 IP 获取用户数据。无法理解如何从回调函数中获取数据并将其插入到我的数据库中。
这是我的代码
const ipstack = require('ipstack');
const ip = '103.195.74.60';
ipstack(ip, process.env.IPSTACK, (err, response) => {
console.log(response);
});
在此处插入查询
// create user
await User.create({
fullName,
phone,
email,
nikeName,
ip,
city (come from ip),
zip (come from ip),
browser,
device,
});
您可以只需要在使用 console.log
调用的位置精确地进行数据库牵引,如下所示:
const ipstack = require("ipstack");
const ip = "103.195.74.60";
ipstack(ip, process.env.IPSTACK, (err, response) => {
// First of all you need to check if there is error
if (err){
// handle error hreer, you might need to use `return`
// at some point to not execute the below code, alternatively
// you could just wrap it in `else`
}
console.log(response); // your orginal `console.log` call
//your database transaction ca be done here,
// create user
await User.create({
fullName,
phone,
email,
nikeName,
ip,
response.city // need to look in documentation
response.zip // or explore the response object terminal
browser,
device,
})
});
我访问响应键的代码部分是推测,您只需要查找 ipstack
包的文档(如果存在),或者只需浏览给您的模式 response
是 console.log
.
最后,我建议使用 official documentation given the lipstick
is not really maintained the last update was 3 years ago, add to that by following the official documentation you could use fetch
or axios
and take advantage of async
await
功能,这是当前处理异步代码的现代方法*。
*这可能是约定俗成的观点。
@Ghassan Maslamani 在按照您的建议进行操作后一直运行顺利。检查代码,如果需要对代码进行任何改进,请指导我。再次感谢。
// get user ip
const ip = req.headers['x-forwarded-for'] || req.socket.remoteAddress;
// location details through Api call
const url = `http://api.ipstack.com/${ip}?access_key=${process.env.IPSTACK}`;
async function locationInfo() {
try {
const result = await axios.get(url);
const data = await result.data;
return data;
} catch (error) {
console.log(error);
}
}
const locInfo = await locationInfo();
// City name
const location = `${locInfo.city},${locInfo.country_name}`;
// zip code
const { zip } = locInfo;