Insert ID twice in the same row.

Hello community!
I’m using insert() for users to input comments to a database. When the comment is created it creates an ID for itself, I want that same ID to be inserted on a different field in the same row, how do I do that?

Here’s what’s going on:

The ‘Comentar’ button is button 8 when clicked whatever is typed in the input box is inserted in the ‘comentarios_1’ field and the user id is inserted in the ‘title’ field when the new row in the database is created. However, I need the ID of the comment that was created to be on the [Conversación] field as well, So I pretty much need it twice in the same row. I hope I make myself clear.

 import wixData from 'wix-data';
  import wixUsers from 'wix-users';

export function button8_click() {
	const currentUser = wixUsers.currentUser;
    if (currentUser.loggedIn) {
    	      currentUser.getEmail().then(email => {
        wixData.query('Afiliados').eq('email', email).find() 
        .then(() => {

    let userId=wixUsers.currentUser.id;
        	
const toInsert = {  	
'comentarios_1':$w('#textBox1').value,
            'title': userId,

        };
        
          // add the item to the collection
          wixData.insert("Comunidad", toInsert)
         .then( () => {
		
              $w('#textBox1').value='';
              	$w('#button8').disable();

            });
            });
            });
            }
           }

Thanx!

Seriously though, how do I do that?

Hi Manny,
Can you please elaborate on the root problem and why do you need it again in another field?
It can be done, but maybe there’s a different solution to your problem.
Thanks!

I need to create a reference field with the comment ID. I’m trying to make a LIKE button for each comment. Likes will be stored in a different database but I need the comment ID to connect to it.

And I’m just experimenting, I really don’t know how to make a LIKE button.

Hello?

So how do you do it then?

Hi Manny,
What you can do is add a data hook which will change the item after it’s being inserted to the database.
Follow this article and add a “afterInsertHook”: Velo: Using Data Hooks | Help Center | Wix.com

In the hook code, do something like this (note you need to change “yourSecondIdField” to your field key):

import wixData from 'wix-data';

export function MyCollection_afterInsert(item, context) {
  item.yourSecondIdField = item._id;
  return wixData.save(context.collectionName, item);
}

Sorry, it’s not working for me. :frowning:
I also tried this:

export function Comunidad_afterInsert(item, context) {
	let toUpdate = item;
	let newTitle = item._id;
	toUpdate.title = newTitle;

 wixData.update("Comunidad", toUpdate)
	.then( () => {
  return wixData.save(context.Comunidad, item);

           });
           }

Help please!

Use the code I suggested and don’t change “context.collectionName”, this variable already contains your actual collection name.

I did try what you suggested but it’s not working. I really don’t know what I’m doing wrong here.
Here’s what I got:

  import wixData from 'wix-data';
  import wixUsers from 'wix-users';

export function button8_click() {
   const currentUser = wixUsers.currentUser;
    if (currentUser.loggedIn) {
    	      currentUser.getEmail().then(email => {
        wixData.query('Afiliados').eq('email', email).find() 
        .then(() => {

    let userId=wixUsers.currentUser.id;
        	      	
  const toInsert = {
  	
  	'comentarios_1':$w('#textBox1').value,
            'title': userId,
        };
        
          // add the item to the collection
          wixData.insert("Comunidad", toInsert)
         .then( () => {

              $w('#textBox1').value='';
              	$w('#button8').disable();

            });
            });
            });
            }
           }
           
export function Comunidad_afterInsert(item, context) {
  item.conversacion = item._id;
  return wixData.save(context.collectionName, item);
}
      

Don’t copy paste the code to the page code. You need to add a data hook which will create a special file where you should out the code. Follow the article I linked to see how to add a hook.

ok I figured it out.Thank you so much for your help!

How you solved the problem?