[SOLVED] - 'Broken Reference' When Inserting New Values

Hello again, guys :raised_hand_with_fingers_splayed:

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

I found lots of similar posts in the forum about the broken reference, but couldn’t find a solution or even one reason that might cause this error to occur.

Here’s one post that Wix admits it was an error from their end, even if it’s old, but can it be a happening again?

https://www.wix.com/corvid/forum/community-discussion/reference-constantly-being-broken

Other old and recent threads that haven’t been answered:
https://www.wix.com/corvid/forum/community-discussion/reference-field-broken
https://www.wix.com/corvid/forum/community-discussion/avoid-reference-is-broken-after-insertion-to-collection
https://www.wix.com/corvid/forum/community-discussion/reference-is-broken

Come on @brett-haralson , we all know you were affected by the pandemic, but it’s been years for this issue and yet no documentation about it exists.

#GoUp

#GoUp again …

Reference Fields need the ID (_id) of the item being referenced.

Let us suppose that I have a database called ’ ipAddress ’ which has all the IPs in the ’ title ’ field.

After this I have another collection called ’ database ’ and the ’ ipDetails ’ field of this collection is the Reference field.

If I use the following code:

wixData.query('ipAddress')
.eq('title', '123.456.789')
.find()
.then( (res) => {
   if(res.items.length > 0) {
   let Item = res.items[0];
   let data = {
      userId: 'My User ID',
      source: 'Fronted Attempt',
      ipDetails: Item.title //reference field
   };
   wixData.insert('database', data)
   .then( () => {
        console.log('done');
   });
 }
});

It will not work because I am passing a string for the reference field (Wrong)

Correct Method:

Pass the Item ID (_id) for the reference field. So I do the below

wixData.query('ipAddress')
.eq('title', '123.456.789')
.find()
.then( (res) => {
   if(res.items.length > 0) {
   let Item = res.items[0];
   let data = {
       userId: 'My User ID',
       source: 'Fronted Attempt',
       ipDetails: Item._id //reference field
   };
   wixData.insert('database', data)
   .then( (result) => {
          console.log(result);
   });
  }
});

& there you go

Hi shan, thank you so much for providing this informative explanation.

You gave me exactly what I was missing, I’ll query the item to get its _id before inserting, I’ll apply the necessary changes and hopefully it will work.

Again, thank you so much for your answer :blush: