I have a select dropdown I'd like to populate with the names of users in objects:
<select id="entityDropDown"
ng-model="selectedUser"
ng-options="user as user.name for user in users"
ng-change="getUserInfo(selectedUser)">
</select>
My object is structured as such:
users: {
1: {
name: Demo Administrator,
id: 1,
domain: null,
email: null,
isAdmin: False,
},
4: {
name: ITUN\WSS,
id: 4,
domain: i:0#.f|admembers|,
email: ,
isAdmin: False,
}
}
Try using the comprehension expression for objects:
ng-options="user as user.name for (key,user) in users"
In your case users is not an array.
So instead of
users: {
1: {
name: Demo Administrator
id: 1
domain: ITUN\demoadmin_compulite
email: simkessy#gmail.com
isAdmin: False
}
}
You want something like this
users: [
{
name: Demo Administrator
id: 1
domain: ITUN\demoadmin_compulite
email: simkessy#gmail.com
isAdmin: False
} ,
{
name: Demo Administrator
id: 2
domain: ITUN\demoadmin_compulite
email: simkessy#gmail.com
isAdmin: False
}
]
Related
I'd like to be able to allow admins to use a single email and have multiple user names (bypassing the unique constraint for email). Moreover, if users aren't admins then they can't bypass the unique constraint.
However, using partialFilterExpression I thought this was the route to go using the roles I have defined (user and admin) but every time I save a new admin user, then when I try to add another admin user with another username (same email) I get the error:
E11000 duplicate key error collection: convofinddb.users index: email_1 dup key: { email: "email#gmail.com" }
User Schema
const userSchema = new mongoose.Schema(
{
firstName: {},
lastName: {},
userName: {
type: String,
required: true,
unique: true,
},
email: {
type: String,
required: true,
index: {
unique: true,
partialFilterExpression: { role: { $eq: 'user' } },
},
},
password: {},
role: {
type: String,
enum: ['admin', 'user'],
default: 'user',
},
},
{
timestamps: true
}
Any ideas?
Looks like I figured it out
I'll need to check if a user's role is an admin
If an admin, create a new user
Then create another email index using partialFilterExpression and { unique: false }
// this data would be from some form...
const userBody = {
firstName: 'John',
lastName: 'Doe',
userName: 'welit9000',
email: 'email#gmail.com',
password: '****',
confirmPassword: '****',
role: 'admin'
}
if (userBody.role === 'admin') {
const user = await UserModel.create(userBody);
await UserModel.collection.createIndex({ email: 1 }, { unique: false, partialFilterExpression: { role: { $eq: 'admin' } } });
}
I have a data structure like this:
{
isReceivesSalary: true,
boardMembers: [
{
name: 'David',
country: 'USA',
additionalProperty: {
isReceivingSalary: true
}
},
{
name: 'Dave',
country: 'France',
additionalProperty: {
isReceivingSalary: false
}
}
]
}
When isReceivesSalary is checked then at least one of the boardMembers should have
property isReceivingSalary should be checked.
Below is my validation schema which is not working.
const boardMembersValidation ={
isReceivesSalary: yup.boolean().required(),
boardMembers: yup.array(yup.object.shape({
name: yup.string().required(),
country: yup.string().required(),
additionalProperty: yup.object().shape({
isReceivingSalary: yup.boolean()
})
}))
};
Is it possible to validate at least one of the boardMember should be receiving salary when isReceivesSalary is true?
Also add validation message.
I have been having some problems with react-hook-form and disappearing defaultValues
So I initiate the useForm with default values, and then get async user location from our API. I want to pre-fill the country based on our user data.
unfortunatelly, after I do that, loose all default values inside payment prop.
why is that happening and how can I fix it?
I tried to find some solutions but none seem to help me (setTimeout, useMemo on defaultValues etc)
const form = useForm<CheckoutFormSteps>({
defaultValues: {
team: {
id: null,
icon: 'url',
name: 'someName',
},
payment: {
address1: '',
address2: '',
city: '',
companyName: '',
paymentMethod: PaymentMethod.CreditCard,
country: '',
firstName: '',
lastName: '',
phone: '',
postalCode: '',
state: '',
vat_number: '',
},
},
});
useEffect(() => {
// some async logic to get user's country
form.setValue('payment.country', 'US');
// also tried setTimeout(() => form.setValue('payment.country', 'AU'));
}, []);
then the getValues look like
{
team: {
id: null,
icon: 'url',
name: 'someName',
},
payment: {
country: 'US',
} // all other payment props are gone
}
Here you go with a solution
const form = useForm<CheckoutFormSteps>({
defaultValues: {
team: {
id: null,
icon: 'url',
name: 'someName',
},
payment: {
address1: '',
address2: '',
city: '',
companyName: '',
paymentMethod: PaymentMethod.CreditCard,
country: '',
firstName: '',
lastName: '',
phone: '',
postalCode: '',
state: '',
vat_number: '',
},
},
});
useEffect(() => {
// some async logic to get user's country
form.setValue({
...form,
payment: {
...form.payment,
country: "US"
}
});
// also tried setTimeout(() => form.setValue('payment.country', 'AU'));
}, []);
Whenever you are setting the value, you need to retain the old value as value. For the same reason please use spread operator.
Let say, I have some documents like -
[
{
fname: "FirstName1",
lname: "LastName1",
email: "a#gmail.com"
},
{
fname: "FirstName2",
lname: "LastName2",
email: "b#gmail.com"
},
...
]
, are already existed in the database. Now I am going to insert multiple documents with insertMany, to that database, where some of the objects contains same email ids, like -
[
{
fname: "FirstName3",
lname: "LastName3",
email: "c#gmail.com"
},
{
fname: "FirstName4",
lname: "LastName4",
email: "a#gmail.com"
},
{
fname: "FirstName5",
lname: "LastName5",
email: "b#gmail.com"
},
...
]
So I need to replace the previous docouments with the other fields which contains same email ids with the inserted one. So I want the databse look like this.
[
{
fname: "FirstName4",
lname: "LastName4",
email: "a#gmail.com"
},
{
fname: "FirstName5",
lname: "LastName5",
email: "b#gmail.com"
},
{
fname: "FirstName3",
lname: "LastName3",
email: "c#gmail.com"
},
...
]
Is there any solution. Please let me know.
Thanks.
You can't do it by insertMany for that you need to use bulk update with upsert option.
Below are the two different syntax to do it, You man need to loop to create the bulk queries. (There may be a typo error, code not tested)
var bulk = db.collection_name.initializeUnorderedBulkOp();
bulk.find( { email: "a#gmail.com" } ).upsert().update( { $set: { fname: "FirstName4", lname: "LastName4", email: "a#gmail.com" } } );
bulk.find( { email: "b#gmail.com" } ).upsert().update( { $set: { fname: "FirstName5", lname: "LastName5", email: "b#gmail.com" } } );
bulk.find( { email: "c#gmail.com" } ).upsert().update( { $set: { fname: "FirstName3", lname: "LastName3", email: "c#gmail.com" } } );
bulk.execute();
Another syntax
var ops = []
objectsArray.forEach(item => {
ops.push(
{
updateOne: {
filter: { email: item .email},
update: {
$set: { fname: item.fname, lname: item.lname, email: item.email }
},
upsert: true
}
}
)
})
db.collections('collection_name').bulkWrite(ops, { ordered: false });
If you are using mongoose check there BulkWrite Documentation
I have formik form with below initialValues
const initialValues1 = {
people: [
{
id: Math.random(),
email: "",
isNewUser: true,
profile: {
firstName: "",
lastName: "",
}
}
]
};
I want to validate firstName and lastName only when is isNewUser is true by Yup I am trying below but it is not working. How can validate conditionally in Formik Yup
people: Yup.array().of(
Yup.object().shape({
isNewUser: Yup.boolean(),
profile: Yup.object().shape({
firstName: Yup
.string()
.when('isNewUser', {
is: true,
then: Yup.string().required("First name is required")
}),
})
})
)
formatted code in IDE
people: Yup.array().of(
Yup.object({
isNewUser: Yup.boolean(),
profile: Yup.object().when('isNewUser', {
is: true,
then: Yup.object({
firstName: Yup.string().required('First name is required'),
})
})
})
);
isNewUser is sibling of profile attribute, so we can use it in when for defining profile schema not it's child(first_name) schema directly.
you can also specify else part using otherwise key
when({ 'attrbute', is: 'func or value', then: 'schema if is true', otherwise: 'schema if is false'})
As per docs ,
Adjust the schema based on a sibling or sibling children fields. You
can provide an object literal where the key is is value or a matcher
function, then provides the true schema and/or otherwise for the
failure condition.
So move the isNewUser to reflect as sibling. Like this:
const initialValues1 = {
people: [
{
id: Math.random(),
email: "",
//isNewUser: true, // <--------------- remove this
profile: {
firstName: "",
lastName: "",
isNewUser: true // <---------------- here.
}
}
]
};