How to set a unique id for an embedded document in Meteor? - meteor

I have setup my collections like this using Simple Schema :
SubLinkSchema = new SimpleSchema({
name: {
type: String,
label: 'Link Name',
unique: false
},
link: {
type: String,
regEx: SimpleSchema.RegEx.Url,
label: 'Custom Link',
optional: true,
autoform: {
class: 'sub-custom-link'
}
}
});
LinkSchema = new SimpleSchema({
name: {
type: String,
label: 'Link Name',
unique: false
},
link: {
type: String,
regEx: SimpleSchema.RegEx.Url,
label: 'Custom Link',
optional: true,
autoform: {
class: 'main-custom-link'
}
},
subLinks: {
optional: true,
label: 'Sub Links',
unique: false,
type: [SubLinkSchema]
}
});
In here, the problem is, the sublinks do not get an ID. Its hard to update them without an id. So, how can I generate a unique ID per sublink (embedded document)?

use an autovalue field in the SimpleSchema
see ref here:
https://github.com/aldeed/meteor-collection2#autovalue
and example:
subLinkID: {
type: String,
autoValue: function() {
return Meteor.uuid();
}
}

It should go with the
Meteor.uuid()

Related

Saving API call result to a collection but recieving errors while setting up a SimpleSchema

I have a Meteor method which does an API call, then the response of the call is saved to the users collection. I'm using the Collection2 package with my project and I'm a little bit lost setting up my SimpleSchema for this.
Here is what the JSON response looks like from the API call:
[{"keyword":"2i","url":"http://example.com","title":"Example","timestamp":"2016-11-05
08:54:42","ip":"00.00.00.000","clicks":"2","user":"HweoSCY2ujscjJ9Zl"},{"keyword":"2j","url":"http://example.com","title":"YouTube","timestamp":"2016-11-06
02:11:18","ip":"00.00.00.000","clicks":"1","user":"HweoSCY2ujscjJ9Zl"},{"keyword":"2k","url":"http://example.com","title":"YouTube","timestamp":"2016-11-08
03:35:12","ip":"00.00.00.000","clicks":"0","user":"HweoSCY2ujscjJ9Zl"}]
Here's currently how I've been able to save this data to the users collection:
Meteor.users.update(Meteor.userId(), {
$set: { 'shortURLs.URLS': result.data }
});
This works and looks like this in the db:
My issue is that I'd like to have a SimpleSchema setup for this so that the "timestamp" will be saved as a Date instead of a String, but everytime I try and create a schema for it I just receive errors like "After filtering out keys not in the schema, your modifier is now empty". I have tried a lot of different variations to try and make it work but none of them have been successful, here's just currently where it's at:
Schema.ShortURLs = new SimpleSchema({
shortURLs: {
type: Object
},
'shortURLs.$': {
type: Object
},
'shortURLs.$.keyword': {
type: String,
optional: true,
label: "Keyword"
},
'shortURLs.$.url': {
type: String,
optional: true,
label: "URL"
},
'shortURLs.$.title': {
type: String,
optional: true,
label: "Title"
},
'shortURLs.$.timestamp': {
type: Date,
optional: true,
label: "Timestamp"
},
'shortURLs.$.ip': {
type: String,
optional: true,
label: "IP"
},
'shortURLs.$.clicks': {
type: String,
optional: true,
label: "Clicks"
},
'shortURLs.$.user': {
type: String,
optional: true,
label: "User"
},
});
This is then attached apart of a User Simple Schema:
...
shortURLs: {
type: Schema.ShortURLs,
optional: true
},
...
And I have that attached to the users collection:
Meteor.users.attachSchema(Schema.User);
I don't think there's an issue with how I have it attached as I have other SimpleSchemas setup the same way and they're working fine, I believe the issue is how I have this particular one written. Any help here would be extremely appreciated.
You need to define the shortURLs type in the Meteor.users collection as type: [Schema.ShortURLs], i.e. a list of type Schema.ShortURLs
Schema = {}
Schema.ShortURLs = new SimpleSchema({
keyword: {
type: String,
optional: true,
label: "Keyword"
},
url: {
type: String,
optional: true,
label: "URL"
},
title: {
type: String,
optional: true,
label: "Title"
},
timestamp: {
type: Date,
optional: true,
label: "Timestamp"
},
ip: {
type: String,
optional: true,
label: "IP"
},
clicks: {
type: String,
optional: true,
label: "Clicks"
},
user: {
type: String,
optional: true,
label: "User"
},
});
Schema.User = new SimpleSchema({
...
shortURLs: {
type: [Schema.ShortURLs],
optional: true
},
...
});
...
Meteor.users.attachSchema(Schema.User);

MeteorJS : Insert objects not work with SimpleSchema

my SimpleSchema.
Products.schema = new SimpleSchema({
_id: {
type: String,
regEx: SimpleSchema.RegEx.Id
},
userId: {
type: String,
regEx: SimpleSchema.RegEx.Id,
denyUpdate: false
},
tags: {
type: Object,
optional: true,
blackbox: true,
label: 'Categoria del producto'
}
});
Product array insert.
Let tags = ["7524", "5249", "7324"];
returns Error :tags product must be an object
with this type of object if it works:
var person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};
but I want to insert an object like this:
let tags = ["7524", "5249", "7324"];
I HOPE YOUR ANSWER AND GREETINGS.
You need to specify the field with the object in this way. Then you can input the array.
Products.schema = new SimpleSchema({
_id: {
type: String,
regEx: SimpleSchema.RegEx.Id
},
userId: {
type: String,
regEx: SimpleSchema.RegEx.Id,
denyUpdate: false
},
tags: {
type: Array,
optional: true,
label: 'Categoria del producto'
},
"tags.$": {
type: Object,
}
});

Meteor collection2 not updating

I'm having some trouble with updating a user account. I use the following schema (collection2):
lib/collections/users.js
Users = Meteor.users;
var Schemas = {};
Schemas.User = new SimpleSchema({
gender: {
type: Number,
min: 1
},
s_gender: {
type: Number,
min: 1,
optional:false
},
picture: {
type: String,
custom: function() {
var base64Matcher = new RegExp("^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{4})$");
var value = this.value.replace("data:image/png;base64,","");
if(!base64Matcher.test(value))
{
return 'no picture';
}
else
{
return true;
}
}
}
});
Users.attachSchema(Schemas.User);
Now I do the update with the following code:
client/templates/start.js
Users.update({_id: Meteor.userId()}, {
$set: {picture: picture, gender: gender, s_gender: s_gender}
}, {validationContext: "updateUser"}, function (error, result) {
if (error) {
errorObjs = Users.simpleSchema().namedContext("updateUser").invalidKeys();
console.log(errorObjs);
}
console.log(result);
});
The validation passes, but I only get a "0" in results (errors are null) - the update isn't working. Errors are shown if I have an empty field, so the validation is working well. If I detach the schema, the update works fine.
Did I forget something here or why isn't he updating when validation passes?
// Edit: Also I see, that Meteor doesn't create users anymore.
I believe you need to use Users.profile.foo instead of Users.foo, because Users is a special meteor collection and you can only save new fields inside the profile field. Try using Simple Schema/Collection 2 suggested User schema as a starting point (I'll copy it bellow). Notice the "profile schema" is loaded before the user schema:
Schema = {};
Schema.UserProfile = new SimpleSchema({
firstName: {
type: String,
regEx: /^[a-zA-Z-]{2,25}$/,
optional: true
},
lastName: {
type: String,
regEx: /^[a-zA-Z]{2,25}$/,
optional: true
},
birthday: {
type: Date,
optional: true
},
gender: {
type: String,
allowedValues: ['Male', 'Female'],
optional: true
},
organization : {
type: String,
regEx: /^[a-z0-9A-z .]{3,30}$/,
optional: true
},
website: {
type: String,
regEx: SimpleSchema.RegEx.Url,
optional: true
},
bio: {
type: String,
optional: true
}
});
Schema.User = new SimpleSchema({
username: {
type: String,
regEx: /^[a-z0-9A-Z_]{3,15}$/
},
emails: {
type: [Object],
optional: true
},
"emails.$.address": {
type: String,
regEx: SimpleSchema.RegEx.Email
},
"emails.$.verified": {
type: Boolean
},
createdAt: {
type: Date
},
profile: {
type: Schema.UserProfile,
optional: true
},
services: {
type: Object,
optional: true,
blackbox: true
}
});
Meteor.users.attachSchema(Schema.User);
source: https://github.com/aldeed/meteor-collection2

Storing arbitrary object inside a field with meteor simple schema

I have a schema with a field type: Object . But whenever i do an insert, that object is empty.
Here is my schema
Contacts.attachSchema(new SimpleSchema({
firstName: {
type: String,
},
lastName: {
type: String,
optional: true
},
twitterFriend: { // this field
type: Object,
optional: true
}
}));
Even if do Contacts.insert({firstName: 'Mustafa', twitterFriend: {test: 'this should be stored'}}) . It does not work.
For an object of arbitrary sub-schema you set blackbox: true
Contacts.attachSchema(new SimpleSchema({
firstName: {
type: String,
},
lastName: {
type: String,
optional: true
},
twitterFriend: { // this field
type: Object,
optional: true,
blackbox: true
}
}));
See SimpleSchema docs for reference.

update working in Robomongo and not from Meteor

This works and insert {name: 'ok', color: '#ff0000'} to the categories when using Robomongo
db.requirements.update({_id: 'kJBZp2gA8TgNX3z2j'}, {$addToSet: {categories: {name: 'ok', color: '#ff0000'}}})
It does not from Meteor.
It inserts an empty object {} into categories array
EDIT
After SimpleSchema.debug = true; I have this log:
SimpleSchema.clean: filtered out value that would have affected key
"categories.$._id", which is not allowed by the schema
same for name and color thus inserting an empty object {}
The schema is:
categories: {
type: [{
_id: {
type: String
},
name: {
type: String
},
color: {
type: String
}
}],
optional: true
}
What would be the correct schema then ?
I don't grasp all the differences, but here is the new schema that now works. Thanks Matt K for the debug option pointer.
'categories.$._id': {
type: String,
optional: true
},
'categories.$.name': {
type: String,
optional: true
},
'categories.$.color': {
type: String,
optional: true
},

Resources