I'm having some troubles understanding, what i believe is trivial but i can't seem to get my head around it.
I have this publish function in server.js (server only)
Meteor.publish("tikiMainFind", function(){
return tikiDB.find()
})
In app.js (server + client) i'm declaring this mongo collection:
tikiDB = new Mongo.Collection("tiki")
Why is it that this doesn't work in client.js
console.log(tikiDB.find())
//ReferenceError: tikiDB is not defined
Without any idea how you have your app structured, I agree with David Weldon's answer. Check File Load Order to see what order your files are getting loaded.
Related
I get this code in my Meteor project, in a client/main.js file
Template.panel.onCreated(function loginOnCreated() {
var profile = Session.get('profile');
this.myvar = new ReactiveVar(User.find({}).fetch());
});
And the result of User.find({}) is empty. If I Query this anywhere else (including meteor mongo) I get an Array of users.
So I wonder if it is a problem with the fact that this code is running in client side. In this same file I get this query working in other places, but probably in the server context.
How can I populate this ReactiveVar with the Mongo result as soon as the Template/page is loaded?
If I do something like in Meteor.startup() at Server side:
console.log(User.find({}).count());
It gives me the correct number of Users. Immediately.
#edit
If I just add a setTimeout of a few seconds (it can't be jsut 1 second, it needs a longet time), it works in this very same place.
Template.panel.onCreated(function loginOnCreated() {
//...
setTimeout(function(){
template.timeline.set(User.find({}).fetch());
console.log(timeline)
},3000);
});
So, anyone knows why it takes so long to allow me to do this operation? Any workaround?
User.find({}).fetch() will give list of users on server side only.
You can probably write a meteor method for fetching the user list on server side and give it call using meteor.call.
In the callback function to this call you can assign the result to desired variable.
I wanna delete record using JS API. In wiki I found example and I know that exist delete() function but I don't know what paramets needed and can't find documentation about ORid realize interface.
Update
When I create or insert function I get error
Internal server error:
java.lang.IllegalArgumentException: Script pool for database 'myDataBase' is not configured
I use this code:
db.save()
//or
db.executeCommand()
Need help. Maybe who know. Thanks
You can use this code for a graph database
var g=orient.getGraph();
var vertex=g.getVertex("your rid");
vertex.getRecord().delete();
and this for a document database
var db=orient.getDatabase();
var document=db.query("select from #21:0");
document[0].delete();
Hope it helps
I am a new user to JavaScript and the meteor framework trying to understand the basic concepts. First of all I want to add a single document to a collection without duplicate entries.
this.addRole = function(roleName){
console.log(MongoRoles.find({name: roleName}).count());
if(!MongoRoles.find({name: roleName}).count())
MongoRoles.insert({name: roleName});
}
This code is called on the server as well as on the client. The log message on the client tells me there are no entries in the collection. Even if I refresh the page several times.
On the server duplicate entries get entered into the collection. I don't know why. Probably I did not understand the key concept. Could someone point it out to me, please?
Edit-1:
No, autopublish and insecure are not installed anymore. But I already published the MongoRoles collection (server side) and subscribed to it (client side). Furthermore I created a allow rule for inserts (client side).
Edit-2:
Thanks a lot for showing me the meteor method way but I want to get the point doing it without server side only methods involved. Let us say for academic purposes. ;-)
Just wrote a small example:
Client:
Posts = new Mongo.Collection("posts");
Posts.insert({title: "title-1"});
console.log(Posts.find().count());
Server:
Posts = new Mongo.Collection("posts");
Meteor.publish(null, function () {
return Posts.find()
})
Posts.allow({
insert: function(){return true}
})
If I check the server database via 'meteor mongo' it tells me every insert of my client code is saved there.
The log on the client tells me '1 count' every time I refresh the page. But I expected both the same. What am I doing wrong?
Edit-3:
I am back on my original role example (sorry for that). Just thought I got the point but I am still clueless. If I check the variable 'roleCount', 0 is responded all the time. How can I load the correct value into my variable? What is the best way to check if a document exists before the insertion into a collection? Guess the .find() is asynchronous as well? If so, how can I do it synchronous? If I got it right I have to wait for the value (synchronous) because I really relay on it.
Shared environment (client and server):
Roles = new Mongo.Collection("jaqua_roles");
Roles.allow({
insert: function(){return true}
})
var Role = function(){
this.addRole = function(roleName){
var roleCount = Roles.find({name: roleName}).count();
console.log(roleCount);
if(roleCount === 0){
Roles.insert({name: roleName}, function(error, result){
try{
console.log("Success: " + result);
var roleCount = Roles.find({name: roleName}).count();
console.log(roleCount);
} catch(error){
}
});
}
};
this.deleteRole = function(){
};
}
role = new Role();
role.addRole('test-role');
Server only:
Meteor.publish(null, function () {
return Roles.find()
})
Meteor's insert/update/remove methods (client-side) are not a great idea to use. Too many potential security pitfalls, and it takes a lot of thought and time to really patch up any holes. Further reading here.
I'm also wondering where you're calling addRole from. Assuming it's being triggered from client-side only, I would do this:
Client-side Code:
this.addRole = function(roleName){
var roleCount = MongoRoles.find({name: roleName}).count();
console.log(roleCount);
if (roleCount === 0) {
Meteor.call('insertRole', roleName, function (error, result) {
if (error) {
// check error.error and error.reason (if I'm remembering right)
} else {
// Success!
}
});
}
}
How I've modified this code and why:
I made a roleCount variable so that you can avoid calling MongoRoles.find() twice like that, which is inefficient and consumes unneeded resources (CPU, disk I/O, etc). Store it once, then reference the variable instead, much better.
When checking numbers, try to avoid doing things like if (!count). Using if (count === 0) is clearer, and shows that you're referencing a number. Statements like if (!xyz) would make one think this is a boolean (true/false) value.
Always use === in JavaScript, unless you want to intentionally do a loose equality operation. Read more on this.
Always use open/closed curly braces for if and other blocks, even if it contains just a single line of code. This is just good practice so that if you decide to add another line later, you don't have to then wrap it in braces. Just a good practice thing.
Changed your database insert into a Meteor method (see below).
Side note: I've used JavaScript (ES5), but since you're new to JavaScript, I think you should jump right into ES6. ES is short for ECMAScript (which is what JS is based on). ES6 (or ECMAScript 2015) is the most recent stable version which includes all kinds of new awesomeness that JavaScript didn't previously have.
Server-side Code:
Meteor.method('insertRole', function (roleName) {
check(roleName, String);
try {
// Any security checks, such as logged-in user, validating roleName, etc
MongoRoles.insert({name: roleName});
} catch (error) {
// error handling. just throw an error from here and handle it on client
if (badThing) {
throw new Meteor.Error('bad-thing', 'A bad thing happened.');
}
}
});
Hope this helps. This is all off the top of my head with no testing at all. But it should give you a better idea of an improved structure when it comes to database operations.
Addressing your edits
Your code looks good, except a couple issues:
You're defining Posts twice, don't do that. Make a file, for example, /lib/collections/posts.js and put the declaration and instantiation of Mongo.Collection in there. Then it will be executed on both client and server.
Your console.log would probably return an error, or zero, because Posts.insert is asynchronous on the client side. Try the below instead:
.
Posts.insert({title: "title-1"}, function (error, result) {
console.log(Posts.find().count());
});
i am using two database adapters with sails.
one for mondoDB and second for mysql.whenever i run command "sails lift".once it gives an error
error: Error: The hook `orm` is taking too long to load.
Make sure it is triggering its `initialize()` callback, or else set `sails.config.orm._hookTimeout to a higher value (currently 20000)
at tooLong [as _onTimeout] (C:\Users\KAMI\AppData\Roaming\npm\node_modules\sails\lib\app\private\loadHooks.js:92:21)
at Timer.listOnTimeout [as ontimeout] (timers.js:110:15
when i rerun sails without changes it gives no error then.how can i avoid this error everytime.this is my 1st experience with sailsjs so any help will be apreciated....
I ran into this problem last night because of a slow internet connection between my laptop and the DB server. My solution was to create a new file in the config directory called orm.js (name doesn't really matter).
Then add the following code:
// config/orm.js
module.exports.orm = {
_hookTimeout: 60000 // I used 60 seconds as my new timeout
};
I also found I had to change my pubsub timeout but that may not be necessary for you.
// config/pubsub.js
module.exports.pubsub = {
_hookTimeout: 60000 // I used 60 seconds as my new timeout
};
Note: The other answer recommends changing the sails files inside the node_modules folder. This is almost always a bad idea because any npm update could revert your changes.
It is likely best to do this on a per env basis. Under config directory, you will have something like:
Then enter, inside module.exports of each:
module.exports = {
hookTimeout: 40000
}
Notice, there is no need for an underscore in front of the attribute name either.
I realise this is quite an old question, but I also had the same problem. I was convinced it wasn't my connection.
My solution is to change your migration option for your models and you'll have a choice of 3
safe - never auto-migrate my database(s). I will do it myself (by hand)
alter - auto-migrate, but attempt to keep my existing data (experimental)
drop - wipe/drop ALL my data and rebuild models every time I lift Sails
Got to config/models.js and in there put:
migrate: 'safe'
or whatever option from above you want to use.
There are two ways, which we can probably call them as:
1- System-wide method: (as #arcseldon has told)
Try to add the hookTimeout key to the project's config/env/development.js or config/env/production.js file. Next almost all the hooks (except some hooks, such as moduleloader) will retrieve the timeout value and consider it for themeselves.
2- Hook specific method: (as #davepreston has told)
create a [module-name].js file in the project's config folder and add _hookTimeout key to it. So, it will lead into assigning the timeout value only to that specific module. (Be careful about the specific json structure for the sails config files.)
Go to you node_modules folder and browse to \sails\lib\app\private
In your case you should go to this folder:
C:\Users\KAMI\AppData\Roaming\npm\node_modules\sails\lib\app\private
Then open the file named loadHooks.js and go to the line that says:
var timeoutInterval = (sails.config[hooks[id].configKey || id] && sails.config[hooks[id].configKey || id]._hookTimeout) || sails.config.hookTimeout || 20000;
Change the last value in this line from 20000 to some higher value and save the file then run your application by "sails lift" as you normally do
NB: you may need to try out a few higher values instead of 20000 until you reach a value that works for you. My application successfully lifted when I changed the value to 50000
Go to models.js file and uncomment migrate: 'alter'
while running sails lift run this command in the command line
sails lift hookTimeout=75000
You can also try to add defaults: { timeout: 30000 } to your hook
Reference: https://sailsjs.com/documentation/concepts/extending-sails/hooks/hook-specification/defaults
I'm trying to put together a small app using meteor, and having a lot of luck. But I can't get the app to use the Twix plugin for momentjs.
Using Meteorite, I added the Momentjs library from Atmosphere, mrt add moment, so no problem there. But there's no twix package in Atmosphere. I used npm to install twix package, but neither meteor no mrt will add twix, responding Package named twix doesn't exist in your meteor installation which I guess makes sense, it being a Nodejs package not a Meteor package. Finally I tried downloading the twix.js file and placing it in different directories, but nothing has worked.
I'm not getting errors with this code:
if (Meteor.isServer) {
Meteor.startup(function () {
var moment = Npm.require('moment');
Npm.require('twix');
});
and I can use the MomentJS library, but not the twix plugin. Writing (within Meteor.isClient)
Template.dayTable.date = function() {
return moment(Session.get('selectedDate')).format("MMM Do YY");
}
works fine, but writing
var t = new moment("1/25/1982 9:30 AM").twix("1/25/1982 1:30 PM");
has the js console tell me Uncaught TypeError: Object [object Object] has no method 'twix'
Is there a secret place I can put the twix.js file for Meteor/MomentJS to use (which seems likely)? Am I not require-ing it correctly? Or what?
Thanks!
Meteor loads javascript in a certain order. See the Meteor docs (search for "load order"). To fix this:
add moment.js to client/lib to ensure it's loaded before twix
add twix.js to the client folder
You can also look into creating a package (which is what Npm.require is intended for) and include that, or rename the files since files are included alphabetically. More info is in the docs.
It's not documented anywhere, but you can also use Twix standalone, like this:
var Twix = Npm.require('twix');
var t = new Twix(firstTime, secondTime);