Can access JSON attributes from Javascript - javascript

my json:
{
locale: "en",
title: "Survey",
focusFirstQuestionAutomatic: false,
pages: [
{
name: "livingEnvironment",
elements: [
{
type: "html",
name: "navigationWarning",
html: "To navigate "
},
{
type: "html",
name: "IntroEnvironment",
html: "We will now ask you questions about your living environment"
},
{
type: "text",
name: "numhousehold",
width: "auto",
title: "How many people (including yourself) lived in your household",
validators: [
{
type: "numeric",
text: "Please enter a number between 1 and 99.",
minValue: 1,
maxValue: 99
}
],
inputType: "number"
},
{
type: "multipletext",
name: "householdtype",
width: "auto",
title: "Of these, how many (including yourself) were:",
items: [
{
name: "children",
inputType: "number",
title: "Children under 18 years old",
validators: [
{
type: "regex",
text: "One of the numbers below is out of range. Please enter 0, a positive number, or leave the box blank.",
regex: "^(\\s*|\\d+)$"
}
]
},
{
name: "adults",
inputType: "number",
title: "Adults between 18-59 years old",
validators: [
{
type: "regex",
text: "One of the numbers below is out of range. Please enter 0, a positive number, or leave the box blank.",
regex: "^(\\s*|\\d+)$"
}
]
},
{
name: "seniors",
inputType: "number",
title: "Seniors (60+)",
validators: [
{
type: "regex",
text: "One of the numbers below is out of range. Please enter 0, a positive number, or leave the box blank.",
regex: "^(\\s*|\\d+)$"
}
]
},
{
name: "disabled",
inputType: "number",
title: "Disabled",
validators: [
{
type: "regex",
text: "One of the numbers below is out of range. Please enter 0, a positive number, or leave the box blank.",
regex: "^(\\s*|\\d+)$"
}
]
}
}
]}
and my javascript is:
function serverValidateQuestion(survey, options) {
console.log('Validation called');
console.log(options.data.householdtype);
console.log(options.data.householdtype.children);
so i can access console.log(options.data.householdtype) and it gives me the list of items in the in household type. However i am trying to access the "children" value input from householdtype. and when i write console.log(options.data.householdtype.children) it gives me
`
Cannot read property of undefined” error. On google console mode,
"console.log(options.data.householdtype)" gives me this :
` "adults
:
"3"
children
:
"3"
disabled
:
"3"
pregnant
:
"3"
seniors
:
"3"
proto
:
Object
but i only want to access the children value and console.log(options.data.householdtype.children) doesn't work. Additionally, console.log(options.data.numhousehold) does give me a value that the user inputted. Also, these value are from an input box that users fill-out during a survey. Help me please.

ok so i figured it out It is because the on the first page of my html file console.log(options.data.householdtype); gives undefined. On second page of my html does it locate the householdtype then it is able to access console.log(options.data.householdtype.children); otherwise it doesn't work.

Related

How Save Nested Array into Mongodb Using Mongoose schema?

Actually, Only allFacilities field makes an error in Schema, Please Someone help by making sure the correct format of the mongoose schema allFacilities field for allFacilities array data
Actually, Only allFacilities field makes an error in Schema, Please Someone help by making sure the correct format of the mongoose schema allFacilities field for allFacilities array data
My data Look Like That
{
"name": "This is Name countru",
"type": "Hotel",
"country": "Bangladesh",
"city": "Chittagong",
"address": "22 D Block, Kolpolok Residense, Bakulia,",
"distance": "Chittagong",
"title": "tis is best hotel",
"desc": "Pamper yourself with a visit to the spa, which offers massages, body treatments, and facials. You can take advantage of recreational amenities such as an outdoor pool, a spa tub, and a sauna. Additional features at this hotel include complimentary wireless Internet access, concierge services, and a banquet hall. Grab a bite at The Exchange, one of the hotel's 3 restaurants, or stay in and take advantage of the 24-hour room service. Relax with your favorite drink at the bar/lounge or the poolside bar. Featured amenities include a business center, dry cleaning/laundry services, and a 24-hour front desk. Free valet parking is available onsite. Make yourself at home in one of the 241 air-conditioned rooms featuring LED televisions. Complimentary wireless Internet access keeps you connected, and satellite programming is available for your entertainment. Private bathrooms with separate bathtubs and showers feature hair dryers and slippers. Conveniences include phones, as well as safes and coffee/tea makers. Distances a",
"cheapestPrice": "105",
"rooms": [
"629dd3bae549560b971612da",
"629dd418e549560b971612df",
"629dd481e549560b971612ea",
"629dd4b3e549560b971612f0",
"629dd4b4e549560b971612f6",
"629dec61e549560b9716139f"
],
"photos": [
"http://res.cloudinary.com/cloudmonzu/image/upload/v1655222493/upload/jxsbi7r26cyqgnwg65ni.jpg",
"http://res.cloudinary.com/cloudmonzu/image/upload/v1655222494/upload/bybbcdb8iluy0t7smf1u.jpg",
"http://res.cloudinary.com/cloudmonzu/image/upload/v1655222493/upload/pzfzk3umv5agumt30ygo.jpg"
],
"allFacilities": [
{
"hotelfacilities": [
"dsds",
" sdsd",
" sdd"
]
},
{
"roomfacilities": [
"sdsd",
" dss",
" dss"
]
},
{
"wellnessSpa": [
"sds",
" dsd",
" sdsd",
" sds"
]
}
]
}
My Mongoose Schema Look Like That
import mongoose from "mongoose";
const HotelSchema = new mongoose.Schema({
name: {
type: String,
required: true,
},
type: {
type: String,
required: true,
},
country: {
type: String,
required: true,
},
city: {
type: String,
required: true,
},
address: {
type: String,
required: true,
},
distance: {
type: String,
required: true,
},
title: {
type: String,
required: true,
},
desc: {
type: String,
required: true,
},
cheapestPrice: {
type: Number,
required: true,
},
rooms: {
type: [String],
},
photos: {
type: [String],
},
allFacilities: {
type: [String],
required: true,
},
rating: {
type: Number,
min: 0,
max: 5,
},
featured: {
type: Boolean,
default: false,
},
});
export default mongoose.model("Hotel", HotelSchema)
use "type: Array" for rooms, photos and allFacilities
name: {
type: String,
required: true,
},
type: {
type: String,
required: true,
},
country: {
type: String,
required: true,
},
city: {
type: String,
required: true,
},
address: {
type: String,
required: true,
},
distance: {
type: String,
required: true,
},
title: {
type: String,
required: true,
},
desc: {
type: String,
required: true,
},
cheapestPrice: {
type: Number,
required: true,
},
rooms: {
type: Array,
},
photos: {
type: Array,
},
allFacilities: {
type: Array,
required: true,
},
rating: {
type: Number,
min: 0,
max: 5,
},
featured: {
type: Boolean,
default: false,
},
});

SurveyJs with Laravel 8 without Larvel-surveyjs package

I want to create a survey app for my client for this I select surveyjs library to build the app.
I have created all tables like Survey, Questions, Answers, and Options now I want to pass dynamics questions and answers from database like
public function my_survey($slug)
{
$surveys = Survey::where('slug',$slug)->first();
$questions = Question::where('survey_id',$surveys->id)->with('answers')->get();
return view('user.my-survey', compact('surveys','questions'));
}
this will return the array of questions and options saved in database. and
this is Surveyjs script
Survey
.StylesManager
.applyTheme("modern");
var json = {
pages: [
{
questions: [
{
type: "matrix",
name: "Quality",
title: "Please indicate if you agree or disagree with the following statements",
columns: [
{
value: 1,
text: "Strongly Disagree"
}, {
value: 2,
text: "Disagree"
}, {
value: 3,
text: "Neutral"
}, {
value: 4,
text: "Agree"
}, {
value: 5,
text: "Strongly Agree"
}
],
rows: [
{
value: "affordable",
text: "Product is affordable"
}, {
value: "does what it claims",
text: "Product does what it claims"
}, {
value: "better then others",
text: "Product is better than other products on the market"
}, {
value: "easy to use",
text: "Product is easy to use"
}
]
}, {
type: "rating",
name: "satisfaction",
title: "How satisfied are you with the Product?",
isRequired: true,
mininumRateDescription: "Not Satisfied",
maximumRateDescription: "Completely satisfied"
}, {
type: "rating",
name: "recommend friends",
visibleIf: "{satisfaction} > 3",
title: "How likely are you to recommend the Product to a friend or co-worker?",
mininumRateDescription: "Will not recommend",
maximumRateDescription: "I will recommend"
}, {
type: "comment",
name: "suggestions",
title: "What would make you more satisfied with the Product?"
}
]
}, {
questions: [
{
type: "radiogroup",
name: "price to competitors",
title: "Compared to our competitors, do you feel the Product is",
choices: ["Less expensive", "Priced about the same", "More expensive", "Not sure"]
}, {
type: "radiogroup",
name: "price",
title: "Do you feel our current price is merited by our product?",
choices: ["correct|Yes, the price is about right", "low|No, the price is too low for your product", "high|No, the price is too high for your product"]
}, {
type: "multipletext",
name: "pricelimit",
title: "What is the... ",
items: [
{
name: "mostamount",
title: "Most amount you would every pay for a product like ours"
}, {
name: "leastamount",
title: "The least amount you would feel comfortable paying"
}
]
}
]
}, {
questions: [
{
type: "text",
name: "email",
title: "Thank you for taking our survey. Your survey is almost complete, please enter your email address in the box below if you wish to participate in our drawing, then press the 'Submit' button."
}
]
}
]
};
window.survey = new Survey.Model(json);
survey
.onComplete
.add(function (sender) {
document
.querySelector('#surveyResult')
.textContent = "Result JSON:\n" + JSON.stringify(sender.data, null, 3);
});
$("#surveyElement").Survey({model: survey});
I want to pass dynamic questions and options from laravel controller to the above script. please help me how to acheive this
In your my-survey view put questions in JSON format:
#isset($questions)
<script type="text/javascript">
window.questions= {!! $questions !!};
</script>
#endisset
In Surveyjs script put:
const json = window.questions;
window.survey = new Survey.Model(json);

Sanity CMS, referencing arrays within a document

My goal is to have a custom internalLink mark that is able to reference not just the post (which is easy enough since its a type: document) but also reference name: sections which returns array of section.js. That way editors can link to any section within a post document. And ideally the editors can also reach other post sections as well, not just the current one open.
I want to be able to reference the following collection of section.js in the reference dropdown:
Now I understand reference only works for type: document. Can I make it work for the children (array or object) of type: post?
My custom block.js
marks: {
...
annotations: [
{
name: "internalLink",
title: "Internal link",
type: "object",
fields: [
{
name: "linkto",
type: "reference",
title: "Link To",
to: [{ type: "post" }],
},
],
},
...
post.js
export default {
name: "post",
title: "Document",
type: "document",
fields: [
...
{
name: "sections",
title: "Sections",
type: "array",
of: [{ type: "section" }],
},
],
}
section.js
export default {
title: "Section",
name: "section",
type: "object",
fields: [
{
name: "sectionTitle",
title: "Section Title",
type: "string",
},
{
title: "Content",
name: "content",
type: "blockContent",
},
],
}
Appreciate any help/pointers I can get :)

Kendo UI Dropdownlist checked

I have a kendo ui function dropdownlist, and it will call at Grid column editor. My question, by default how to display "Yes" when Add New Record in edit function. Currently it display null when Add New Record.
Demo in Dojo
Here I provide a working demo. Thank You
If I understand correctly you only have to add a default value to the Price in the Model?
"Price": {type: "string", defaultValue: "y" },
I include the whole function, just in case:
$(function() {
$("#grid").kendoGrid({
dataSource: {
data: [
{ Name: "Young John", Price: "y" },
{ Name: "John Doe", Price: "n" },
{ Name: "Old John", Price: "y" }
],
schema: {
model: {
id: "id",
fields: {
"id": { type: "number" },
"Price": {type: "string", defaultValue: "y" },
}
}
}
},
editable: "inline",
toolbar: ["create"],
columns: [{ field: "Name"},
{ field: "Price",
template: "#=(data.Price == 'y' ? 'Yes' : 'No')#",
editor: radioPrice
} ],
edit: function(e) {
if (e.model.isNew()) {
e.model.Price = 'y';
}
}
});
});

Simple Schema error in Meteor

I am using the meteor accounts and simple schema for users to register and enter their profile information in my app. I defined a schema for users and attached a schema for address to it. However, when I go to register an account, I keep getting the error "Address required" although I filled out all the required fields for address. Here's my schema:
Schemas.UserAddress = new SimpleSchema({ //UserAddress schema defined
streetAddress: {
type: String,
max: 100,
optional: false
},
city: {
type: String,
max: 50,
optional: false
},
state: {
type: String,
regEx: /^A[LKSZRAEP]|C[AOT]|D[EC]|F[LM]|G[AU]|HI|I[ADLN]|K[SY]|LA|M[ADEHINOPST]|N[CDEHJMVY]|O[HKR]|P[ARW]|RI|S[CD]|T[NX]|UT|V[AIT]|W[AIVY]$/,
optional: false
},
zipCode: {
type: String,
regEx: /^[0-9]{5}$/,
optional: false
}
});
var Schemas = {};
Schemas.UserProfile = new SimpleSchema({
companyName: {
type: String,
regEx: /^[a-zA-Z-]{2,25}$/,
optional: false
},
tireMarkup: {
type: Number,
optional: true,
min: 1
},
phoneNum: {
type: String,
regEx: /^[(]{0,1}[0-9]{3}[)]{0,1}[-\s\.]{0,1}[0-9]{3}[-\s\.]{0,1}[0-9]{4}$/,
optional: false
},
confirmed: {
type: Boolean,
optional: true
},
});
Schemas.User = new SimpleSchema({
emails: {
type: [Object],
optional: false
},
"emails.$.address": {
type: String,
regEx: SimpleSchema.RegEx.Email
},
"emails.$.verified": {
type: Boolean
},
createdAt: {
type: Date
},
profile: {
type: Schemas.UserProfile,
optional: false
},
address: { //Attach user address schema
type: Schemas.UserAddress,
optional: false
},
services: {
type: Object,
optional: true,
blackbox: true
}
});
Meteor.users.attachSchema(Schemas.User);
Here is my accounts config which creates the sign up form:
Meteor.startup(function () {
AccountsEntry.config({
logo: '', // if set displays logo above sign-in options
termsUrl: '/terms-of-use', // if set adds link to terms 'you agree to ...' on sign-up page
homeRoute: '/', // mandatory - path to redirect to after sign-out
dashboardRoute: '/dashboard/', // mandatory - path to redirect to after successful sign-in
profileRoute: '/profile',
passwordSignupFields: 'EMAIL_ONLY',
showOtherLoginServices: true, // Set to false to hide oauth login buttons on the signin/signup pages. Useful if you are using something like accounts-meld or want to oauth for api access
extraSignUpFields: [
{
field: "companyName",
label: "Company Name",
placeholder: "Petrocon",
type: "text",
required: true
},
{
field: "firstName",
label: "First Name",
placeholder: "John",
type: "text",
required: true
},
{
field: "lastName",
label: "Last Name",
placeholder: "Nguyen",
type: "text",
required: true
},
{ field: "streetAddress",
label: "Street Address",
placeholder: "12345 Main Street",
type: "text",
required: true
},
{ field: "city",
label: "City",
placeholder: "Springfield",
type: "text",
required: true
},
{ field: "state",
label: "State",
placeholder: "Missouri",
type: "text",
required: true
},
{ field: "zipCode",
label: "Zip Code",
placeholder: "65801",
type: "text",
required: true
},
{
field: "phoneNum",
label: "Contact Number",
placeholder: "316-000-0000",
type: "text",
required: true
}
]
});
});
As you can see, I defined all of the fields in the address schema in AccountsEntry.config.. therefore I don't know why when I enter all the information in the form, I get the error 'address required'. Does anyone know what's going on? Thank you in advance!
Looks like you're using the accounts-entry package. You can see the code where the account is created in this file:
https://github.com/Differential/accounts-entry/blob/9aac00cb3c67afcbb1cc990c7af1f2c7607a2337/server/entry.coffee
Line 29 looks like this:
profile: _.extend(profile, user.profile)
If you debug and look at the value of profile after the extend method is called, you'll see this:
{
"companyName": "Petrocon",
"firstName": "John",
"lastName": "Nguyen",
"streetAddress": "12345 Main Street",
"city": "Springfield",
"state": "Missouri",
"zipCode": "65801",
"phoneNum": "316-000-0000"
}
There is no property named "address" for your Schemas.User schema.
There might be some slick way to do what you're looking for, but the easiest approach is to just get rid of the Schemas.UserAddress schema. Move those properties (streetAddress, city, state, zipCode) into the Schemas.UserProfile schema and it will work.

Categories