Hello again, guys
I’m trying to insert an IP address in a custom ’ Analytics ’ database, but I get a ’ Broken Reference ’ error in the database.
How does it work? An external function fetch the IP address of the visitor, check if the IP address is blocked or not , if it is, kick the visitor out of the website, if it’s not, check if there’s an existing record of the IP in the local database , if there is, just set the IP Address (the main field of the every relevant database) on the referenced field while inserting the new Analytics session to the database.
The thing is that I’m getting a ‘Broken Reference’ even though the main fields are similar to the value that I’m trying to insert.
As the picture shows (A picture from the Analytics database), the IP Address is inserted correctly, the 'IP Details - ipDetails) is a reference field to another database (IP Details), the main field in its database is ‘ipAddress’ which is the same as the analytics main field, and I used the same object to insert it in the two of the collections.
Here’s a code from the Backend :
import wixData from 'wix-data';
let analytics = { returningIP: {}, newIP: {} }
export async function saveIPDetails(json, userId, data) {
let options = {
"suppressAuth": true,
"suppressHooks": true
};
return wixData.query('BlockedIPs').eq('ipAddress', json.query).find(options).then(async (result) => {
if (result.items.length > 0) {
if (result.items[0].blocked) {
return 'blocked';
}
} else {
return wixData.query('IPAddresses').eq('ipAddress', json.query).find(options).then(async (result2) => {
if (result2.items.length > 0) {
analytics.returningIP = {
ipAddress: json.query, // Text field
ipDetails: json.query, // Reference field
blocked: null, // Reference field
uniqueVisit: false,
}
return wixData.insert('Analytics', analytics.returningIP, options).then(() => {
return 'ok';
}).catch((err) => {
let error = {
message: err.message,
code: err.code,
details: 'An error occurred when trying to save the new analytics data'
}
return error
})
} else {
let ipDetails = {
city: json.city,
country: json.country,
ipAddress: json.query, // Text field (Main field)
}
return wixData.insert('IPAddresses', ipDetails, options).then(() => {
analytics.newIP = {
ipAddress: json.query, // Text field (Main field)
ipDetails: json.query, // Reference field
blocked: null, // Reference field
uniqueVisit: true
}
// insert the new analytics with the unique visit true and referenced IP
return wixData.insert('Analytics', analytics.newIP, options).then(() => {
return 'ok';
}).catch((err) => {
let error = {
message: err.message,
code: err.code,
details: 'An error occurred when trying to save the new unique analytics data'
}
return error
})
}).catch((err) => {
let error = {
message: err.message,
code: err.code,
details: 'An error occurred when trying to save the new IP details data'
}
return error
})
}
}).catch((err) => {
let error = {
message: err.message,
code: err.code,
details: 'An error occurred when trying to query the IP addresses database'
}
return error
})
}
}).catch((err) => {
let error = {
message: err.message,
code: err.code,
details: 'An error occurred when trying query the blocked IPs database'
}
return error
})
}
SIDE NOTE: The code style looks weird, sorry, I don’t know what’s wrong with it.
NOTE: I did copy only the relevant code.
If I inserted the IP (192.168.1.1) - I know it’s not a public IP, just an example - into the collection ‘IP Addresses’ as the main field, and then inserted the same value into the ‘Analytics’ field which is a reference to the first collection, it should work right? Then why I get a ‘Reference Broken’ error?
Sorry for the long post, but no one will be able to answer or explain without enough information about what I want to achieve and how.
I appreciate any explanation and even reading this long post.
Ahmad