Insert Url from Dynamic Page into collection

Hi,

I’m trying to get the url from a dynamic page and then insert it (along with the user Id) into a collection when clicking a submit button. Somehow the _Id and the owner are being inserted but the url is not.

Thanks for the help

Here’s the code i’m using

import wixUsers from ‘wix-users’;
import wixLocation from ‘wix-location’;
import wixData from ‘wix-data’;

export function applyCampaign_click(event) {
//Add your code for this event here:

let user = wixUsers.currentUser;
let userId = user.id; // “r5cme-6fem-485j-djre-4844c49”
let isLoggedIn = user.loggedIn; // true
let url = wixLocation.url

let toSave = {
“Id”: userId,
“Url”: url,
};

wixData.save(“#InfCampReq”, toSave)
.then( (results) => {
let item = results; //see item below
} )
. catch ( (err) => {
let errorMsg = err;
} );

}

I’m surprised that that anything got inserted at all. You used the field name instead of the field key (which never starts with a capital letter).

Sorry I copied the wrong code

import wixUsers from ‘wix-users’;
import wixLocation from ‘wix-location’;
import wixData from ‘wix-data’;

let url = wixLocation.url;
let user = wixUsers.currentUser;
let userId = user.id; // “r5cme-6fem-485j-djre-4844c49”
let isLoggedIn = user.loggedIn; // true

let toSave = {
“_id”: userId,
“title”: url,
};

export function applyCampaign_click(event) {
//Add your code for this event here:

wixData.save(“#InfCampReq”, toSave)
.then( (results) => {
let item = results; //see item below
} )
. catch ( (err) => {
let errorMsg = err;
} );

}

This also makes no sense, as the _id field is a system field and you can’t set it yourself.

@jonatandor35 You mind giving me a hand with the code? I’m trying to get the url from the dynamic page to be inserted into the title field

@andres so create another field in your collection and name it userId.
Then do:

 let toInsert = { "userId": userId, "title": url }; 
 export function applyCampaign_click(event) {
  wixData.insert("InfCampReq", toInsert) 
  .then( (results) => {
  let item = results;
  } )
  .catch( (err) => {  let errorMsg = err; 
 } );
 }

@jonatandor35 Thanks I will try and let you know!

Hey JD, I tried the code but still get empty fields . Don’t get’s wrong

import wixUsers from ‘wix-users’;
import wixLocation from ‘wix-location’;
import wixData from ‘wix-data’;

let url = wixLocation.url;
let user = wixUsers.currentUser;
let userId = user.id; // “r5cme-6fem-485j-djre-4844c49”
let isLoggedIn = user.loggedIn; // true

let toInsert = {“userId”: userId, “title”: url};

export function applyCampaign_click(event) {
wixData.insert(“#InfCampReq”, toInsert)
.then( (results) => {
let item = results;
} )
. catch ( (err) => { let errorMsg = err;
} );
}

@andres check you field keys and make sure they’re correct. In the screenshot I can only see the field names, but the field keys are more interesting.

@jonatandor35 I believe they match

@andres so I don’t know. your code looks fine. Make sure you set the collection permissions right.

  • you have a mistake in the collection name which is not supposed to include #

@jonatandor35 hole shit that # was the entire mistake!! Thanks man. Much much appreciated!!

@andres you’re welcome :slight_smile:

1 Like

@jonatandor35 Hello, I have similar issue, maybe more easier. I am using WIX Form for inquiries on each Product page. When somebody submitting the Form I need to know for which product he is interested in. Here will be the solution to get together with the message (inquiry) the URL or the name of the product from the page where it was submitted. I cant programming the code, hopefully you can help me with creating the ready code. We also can talk about the freelancer task fr some budget.