Counting files with metadata in a GridFS - javascript

I've been trying to implement a feature into a website that displays the number of unread files in a GridFS. Each file, when stored, is defined with the metadata completed: "false". In the following helper function, I try to find the number of files that are still labeled with this data.
incomplete_diagnostics_count: function() {
return Diagnostics.find({metadata: {completed: "false"}}).count();
},
As of now, this code doesn't work. How would I go about fixing this?

There wasn't a distinct answer anywhere on the internet, but apparently it was just a syntax error:
incomplete_diagnostics_count: function() {
return Diagnostics.find({},{metadata: {completed: "false"}}).count();
},
The extra bracket in the find() dependents fixed it.

Related

Next js, Lottie Cannot add property completed, object is not extensible

I have a problem with Lottie's animation.
I fetch a JSON file (Lottie Animation) from Contentful, and I want to display it by Lottie Component.
But I receive an error: "TypeError: Cannot add property completed, the object is not extensible".
I don't know why I receive this error because, when I add a local JSON file from /public to property 'animationData', all works fine.
I tried everything. But local files always work well but json from cms does not.
Contentful also returned proper data
first of all it would have been better if you posted you code ss here but nevertheless i can still guess it :D, i have worked previously with lottie on react apps, so,
have you downloaded from lottiefiles because you have written "from contentful", i dont know what is that but as long as you are using the predefined method it comes along with to run a JSON lottie it should be working fine.
"when I add a local JSON file from /public to property 'animationData', all works fine." , try tinkering with the method's attributes, see if it comes.
// const defaultOptions = {
// loop: true,
// autoplay: true,
// animationData: breakingnews,
// rendererSettings: {
// preserveAspectRatio: "xMidYMid slice"
// }
// };
this was a piece i used in mine,
if this doesn't answer your question experiment on it, i'm sure will find something. happy coding. :)

page.addScriptTag() causing server errors and alarms

I'm making a bot that could go though my clients website and get details for the products they have and since the products' pricing, availability, promos & etc. changes multiple times in a week then I need to update my spreadsheet too manually for the rest of my team to use. Finding those changes for each product is my job which is very prone to error. If only they could provide me with an API, life would have much been easier but no.
My client told me that I've been spamming their error report and tripping their alarms on their server error logs and the functions/files they found are the helper functions that I injected into the DOMWorld on their site using page.addScriptTag('./client/helper.js') and the helper functions inside that file (wasn't sure if that is all they found but so far they have only mentioned that file and the functions inside it).
const product_name = await page.$eval('#product-name', product => {
getProductName(product )
})
The getProductName() function comes from this file ./client/helper.js.
//This simply removes unwanted characters
function getProductName(product ) {
return product.innerHTML.replace(/[\s]{2}/g,'')
}
I wasn't aware that they could see this as I thought puppeteer runs locally on the client-side, on my computer only.
I have few ideas that could make this go undetected and silence their alarms and errors.
First would be to NOT use page.addScriptTag('./client/helper.js') at all and put the codes of this file straight into the main file then do further processes after getting the innerHTML.
const product_name = await page.$eval('#product-name', product => {
return product.innerHTML
})
const final_product_name = product_name.replace(/[\s]{2}/g,'')
This does work but my only concerns is if will this still be detectable as I am still running an anonymous function inside page.$eval(). That I am still trying to find out if my client complains again.
So my question really is..
Will removing page.addScriptTag() and using anonymous function be undetectable?
is there any other way to scrape string in the DOMWorld ? (if it turns out that anonymous function still triggers errors)
Is this because i'm using headless chrome?
Thank you for reading.

Loading one JS file before another

Ok so I have a .js file with about 10k lines of code. This code can be split up in
sub-object definitions
container object definitions
initialization code (after the objects have been defined)
program functionality
I would like to split this one file into 4 separate files, because it provides a better oversight. How do I go about doing this, given that they absolutely have to be declared in that order? What should I wrap up in a $(document).ready() and what not?
If I just separate the files and link them to the html in the correct order, I get undefined object errors. I was also thinking of something like this; but I don't know if that's any good...
Second JS File
function initializeContainers() {
var containerObj1 = {
bla: 'bla',
bla2: 'bla2'
},
var containerObj2 = {
bla: 'bla',
bla2: 'bla2'
};
};
First JS File
$(document).ready(function() {
function initializeSubObjects(callback) {
var subObj1 = {
somekey: 'somevalue',
someke2: 'someothervalue'
};
callback();
};
initializeSubObjects(initializeContainers);
});
I have no clue whether this is the correct way to do it?
PS: I also know you can add the script tags dynamically; but is that good practice?
In your example, you should swap the contents of your first and second file. You should only call the initializeContainers method when you know for sure the file has been loaded.
The easiest way to think about this is to load all files with definitions first (helpers, functions, classes, ...). Once all these are loaded, put the rest in the last file and start executing the code only in the last file
On a side note: If you deploy this into a production environment, you should consider bundling these files. Downloading 4 files will impact your load time, so it's better to just bundle them together and send them over as a single file. While you're at it, you probably also want to minify it.

Any way to inject values into Less files in Meteor?

I am working on a project where we want the user to be able to define custom colors. We are running the latest version of Meteor with, among others, the less package.
Right now all colors are variables located in a single theme.lessimport file which is included early in processing. All colors throughout the site (and many subsequent less files) are generated from these few variables.
The idea was to just generate a new userTheme.lessimport file for each user that, if present, could be imported just after the theme.lessimport file to override the variables with custom values. It all works beautifully and flawlessly if you physically add the file to the directory, but I can't seem to even think of a way to do it dynamically/programmatically.
I'm starting to wonder if this can even be done with less.
one of the big hang-ups is that so much of the css is derived from these variables—including CSS included with our own app's plugins/modules.
it appears that you can't import a remote file for inclusion in less pre-processing... so the file can't be generated on a remote server (this would be the ideal situation for our situation as user data will exist on an API server).
there doesn't seem to be any programmatic way to generate or otherwise inject any values into less—at least on Meteor—as I can't find any way to interact with the less through JS.
Aside from this inconvenience, less has been perfect for what we're doing, so I really want to make this work. Hoping someone out there has some wisdom or direction they can impart.
Take a look at how the bootstrap3-less package implements variables and mixins. Specifically the Advanced Usage section of their README.
"If you want to #import a file, give it the extension .import.less to prevent Meteor from processing it independently." So in your instance you'll name your theme file: theme.import.less
Of course you can do it. Just use the "fs" node module.
Here's a rather stupid example. There are lots of gotchas when you do it, but for a basic proof-of-concept, check this.
if (Meteor.isClient) {
Template.hello.greeting = function () {
return "Welcome to less_injector_meteor_test.";
};
Template.hello.events({
'click #button': function () {
var css = "body {background: " + $("#color").val() + ";}";
Meteor.call("writeToUserThemeFile", css);
}
});
}
if (Meteor.isServer) {
Meteor.methods({
"writeToUserThemeFile" :function(css) {
var fs = Npm.require("fs");
var path = "/Users/charnjitsingh/Desktop/less_injector_meteor_test";
fs.writeFile(path+"/user_theme.less", css, function(err) {
console.log("WRITING FILE");
if (err) {
console.log("ERROR WHEN WRITING", err);
}
});
}
});
}

Delete cascade JayData

Following my previous question here, is there a way to have the same behaviour as ON DELETE CASCADE (MySQL) with JayData?
If I delete a Test I would like all linked Chapters and Checks to be deleted. I tried this code:
myDB.onReady(function(){
myDB.Tests.filter(function(test) { return test.Name == this.Name; }, {Name: myTest.Name}).forEach(function(test){
console.log('Starting to remove '+test.Name);
myDB.Tests.remove(test);
myDB.saveChanges(function() {
console.log(test.Name+' removed');
});
});
});
But it doesn't delete the children. Could it be linked with the declaration of Chapters and Checks in the context? JayData probably doesn't see them as children but as independent entities.
I've also seen somewhere that there is some configuration required to do cascading operations with SQLite. I guessed JayData would deal with that.
There is no such functionality in JayData. WebSQL/sqlite has this function built-in but indexedDb hasn't. We could implement it in indexedDb but nobody has asked for it. If you need it then please either add it to our backlog or create an issue on github.

Categories