So I'm using code to inject all inputs into a collection, and I have an Upload button that allows users to upload documents. However, in the inject, I entered upload: $w('#upload').value, which rather than storing the document itself in the collection, it just stores the name. How do I store the document itself in the collection? I saw startUpload( ) on the API, but my issue is that i need to be able to inject this outside the .then function because I have a lot of other stuff to inject to the collection.
Here is my code:
import wixData from 'wix-data'; import wixUsers from 'wix-users'; import wixWindow from 'wix-window'; import { doRegistration } from 'backend/register'; let user = wixUsers.currentUser; let isUserLoggedIn = user.loggedIn; $w.onReady((function () { if (isUserLoggedIn) { $w("#password").collapse(); $w("#title").text = "Sign NDA"; user.getEmail() .then((email) => { $w("#email").value = email; }); } })); function validateRequiredFields(fieldKeys) { let validationPassed = true; fieldKeys.forEach(fieldKey => { let field = $w('#' + fieldKey); if (field.validity.valueMissing) { field.updateValidityIndication(); validationPassed = false; } }); return validationPassed; } function clearFields(fieldKeys) { fieldKeys.forEach(fieldKey => { let field = $w('#' + fieldKey); if (field.type === '$w.SignatureInput') { field.clear(); } else { field.value = null; } if (field.validity.valueMissing) { field.resetValidityIndication(); } }); } export function signupButton_click(event) { let termsIsChecked = $w("#acceptTerms").checked; let OpportinitiesIsChecked = $w("#futureOpportunities").checked; if ($w("#futureOpportunities").checked) { $w("#futureOpportunities").value = "Yes"; } else { $w("#futureOpportunities").value = "No"; } const item = { firstName: $w('#firstName').value, lastName: $w('#lastName').value, email: $w('#email').value, company: $w('#company').value, position: $w('#position').value, date: $w('#date').value, upload: $w('#upload').value, url: $w('#url').value, phone: $w('#phone').value, acceptTerms: $w('#acceptTerms').value, futureOpportunities: $w('#futureOpportunities').value, signature: $w('#signature').value } const keys = Object.keys(item); if (validateRequiredFields(keys)) { wixData.insert('nda', item) .then((results) => { $w('#successMessage').show(); $w('#validationMessage').hide(); clearFields(keys); }) .catch((error) => { console.log(error); }); } else { $w('#validationMessage').show(); } if (isUserLoggedIn) {} else { let firstName = $w("#firstName").value let lastName = $w("#lastName").value let company = $w("#company").value let email = $w("#email").value let password = $w("#password").value let position = $w("#position").value let phone = $w("#phone").value; doRegistration(email, password, firstName, lastName, company, position, phone) .then((result) => { let userId = result.user.id let userInput = { "firstName": firstName, "lastName": lastName, "company": company, "email": email, "position": position, "phone": phone, "_id": userId } wixWindow.openLightbox("Confirm your email") }); } } export function signature_change(event) { $w('#validationMessage').hide(); $w('#successMessage').hide() }
Hi Annita, My code may help you. //Image Uploader export function ImageUploader_change(event) { $w("#UploadStatus").expand(); if($w("#ImageUploader").value.length > 0) { $w("#UploadStatus").label = "Uploading " + $w("#ImageUploader").value[0].name; $w("#ImageUploader").uploadFiles() .then( (uploadedFiles) => { const language = wixWindow.multilingual.currentLanguage; if (language === "en"){ $w("#UploadStatus").label = "Upload successful"; } if (language === "af"){ $w("#UploadStatus").label = "Suksesvol opgelaai"; } }) .catch( (uploadError) => { $w("#UploadStatus").label = "File upload error"; console.log("File upload error: " + uploadError.errorCode); console.log(uploadError.errorDescription); }); } else { $w("#UploadStatus").label = "Please choose a file to upload."; } let debounce clearTimeout(debounce) debounce = setTimeout(async () => { await $w('#Documents').refresh() await $w("#table12").refresh(); }, 2000) if($w('#conditions').text === null || $w('#conditions').text === ""){ $w('#submitButton').expand(); } if($w('#conditions').text !== null || $w('#conditions').text !== ""){ $w('#agreeToTerms').expand(); } } //End of Image Uploader
I added this after the wixData.insert but it still is not working :/ what am I doing wrong here?
if ($w("#upload").value.length > 0) { console.log("Uploading " + $w("#upload").value[0].name + "- Please wait."); $w("#upload").startUpload() .then( (uploadedFile) => { console.log("Upload successful. File is available here:"); console.log(uploadedFile.url); const userNda ={upload: uploadedFile} wixData.update('nda', userNda ); console.log( "Updated"); }) }