I am working for a client developing a form that a user fills out and submits it so that a dynamic page is created with their filled out information. Upon hitting the submit button I would like the page to redirect to the newly created dynamic page and send out an email to the user. So in Velo I have written the following on the front end of the page:
import wixLocation from 'wix-location';
import wixData from 'wix-data';
import { updateCollectionVault } from 'backend/email';
import { sendEmail } from 'backend/email';
let baseURL = wixLocation.baseUrl;
$w.onReady(async function () {
$w('#dataset1').onReady(async function() {
$w('#dataset1').onAfterSave(async function(beforeItem, afterItem) {
$w('#stripTimer').hide();
console.log("the item just saved is: ", afterItem);
let vaultNumber = await updateCollectionVault(afterItem._id, afterItem.databaseName);
console.log('the vaultNumber is: ', vaultNumber);
let newURL = afterItem['link-storytime-all'] + vaultNumber; wixLocation.to(newURL);
})
})
});
So vaultNumber should be receiving a uniquely created number that is assigned to the dynamic page’s url. But I keep getting ‘undefined’ on the console.log statement.
Here is the backend code:
export async function updateCollectionVault(id, collection) {
let newVault = 0;
console.log("inside the backend update with id and collection: ", id, collection);
let query = await wixData.query("test")
.descending("vaultNumber")
.limit(1)
.find()
.then(r => {
let vault = r.items[0];
console.log("the item we found in test is: ", vault);
let vaultn = vault.vaultNumber + 1;
newVault = vaultn;
console.log("the new vault number is: ", vaultn);
let toInsert = {
"vaultNumber": newVault,
"pageId": id,
"collectionName": collection //For each dynamic page and form,
//this has to be changed
};
//this creates the new vaultnumber in test database
wixData.insert("test", toInsert).then((results) => {
let item = results; //see item below
//add the new vault number to the page itself
console.log("inserted a new item to test: ", item);
wixData.query(item.collectionName)
.eq('_id', item.pageId)
.find()
.then((result) => {
console.log("found the new entry in the collection: ", result);
result.items[0].title = item.vaultNumber.toString();
wixData.update(item.collectionName, result.items[0])
.then((other) => {
console.log("added the vault number to the page's properties: ", other.title);
return other.title;
})
.catch(error => {
console.log("Save error 1", error);
});
})
})
.catch((err) => {
let errorMsg = err;
});
});
}
This console.log("added the vault number to the page’s properties: ", other.title); prints out the new number “233” for example. But when it gets to the return other.title, on the front end I am receiving an undefined.
Could anybody help me figure out why?
Developer Tools Console
Wix logs