How to check express local variable exists when in script tag of jade? - node.js

I have a data variable sent to client-side, but it may not always be included as a variable in the express locals. If it doesn't exist, var data = !{JSON.stringify(data)}; returns var data = ; which causes a js error.
I've tried using conditionals prefixed with '-' but that doesn't seem to work.
script(type='text/javascript')
- if locals.data
var data = !{JSON.stringify(data)};
- else
var data = {};
How do I give it a default if locals.data is undefined?

Don't you hate it when you wrack your brain, then ask for help on SO, only to figure it out 5min later...
Looks like the following keeps the jade and javascript happy:
var data = !{ JSON.stringify(locals.data || '') };

Related

How can I check if a global variable that defined in Node.js if it's not changing after seconds

I wrote an app in nodejs but having logic problem with variable.
I'm looking for help with this issue.
Well, i have a global integer variable named "global_something"
I need a function that check if "global_something" not changing after 1000ms or 2000ms
How i can do that ?
Thanks!
You have to make 2 variable for that
var original_global = 'Test';
var copy_original_global; // save this field in database or json file for compare
setInterval(()=>{
if(!copy_original_global){
copy_original_global = original_global;
return
}
if(original_global != copy_original_global){
console.log('Changed')
}
}, 3000);

module.exports variable producing undefined result

So one of the features of the bot I am working on is that I can be on discord 'discreetly', meaning that I can have the idle status but if a friend knows what command to call, they can actually check if I am there. So in my index file I am using module.exports to store the variable that will contain the info that I set. In the other file, I have an array of values that depending on the value from the variable, the bot will respond with one of the phrases from the array. The problem is that when using the variable, I get an undefined response. Any ideas what I am doing wrong? Important to note
I have checked to make sure by putting an actual number and have gotten the correct response so it is an issue with the exporting. I also have the correct file path. I have also assigned variable info a number and gotten the same result. Edit: tried using the filepath as part of the variable in the array like so and got the same error
//This got me a new result so progress.
const filepath = filepath;
console.log(filepath); //This gets me {}
message.reply(activity[info]); //undefined
//new attempt that failed
//paraphrasing the filepath assignment
const filepath = filepath;
activity[filepath.info]
//first attempt
//from index
var info = message.content;
module.exports = info;
//from the other file
var activity = ["Ready to play","Chilling","Doing work","afk","can talk"];
console.log(activity[info]);
message.reply(activity[info]);
how to get a variable from a file to another file in node.js
So this is the solution to my problem
//index
var info = message.content;
module.exports.info = message.content;
//other file
const filepath = filepath;
var activity = [array of different values];
message.reply(activity[index.info]);
Thank you slothiful for you time in trying to help me. I really appreciate it

Configuring $.ajax with backbone on node for testing with vows

(Edited to greatly simplify)
On node I have the following server.js file.
var Backbone = require('backbone');
var Tweet = Backbone.Model.extend({});
var Tweets = Backbone.Collection.extend({
model : Tweet,
url: function () {
return 'http://search.twitter.com/search.json?q=backbone'
}
});
var myTweets = new Tweets();
myTweets.fetch();
When I run this, I get an error that says. "Cannot call method 'ajax' of undefined" (1359:14)
basically that is the result of $ being undefined. Why is it undefined? Well there are a number of intermediate steps but when the file is loaded, it is expecting "this" to be "window" in browser or "global" on server. executed on node "this" = {}.
So the question, "How do I set 'this' to global" inside the backbone.js file?
On Backbone >= 1.x, you can simply assign Backbone.$ rather than using Backbone.setDomLibrary.
Solution for Backbone < 0.9.9
The first issue you need to address is how you are running this on Node anyway. Nodejs is a server-side JS environment, but it does not include any logic for controlling a DOM. For that you need to load something like JSDom.
When you have some DOM environment set up, you can load jQuery and your code into it and it should work just like a browser.
To answer your question specifically though, loading jQuery into the global is a bit of an ugly way to do it. You should use Backbone's setDomLibrary function to set $ to what you want.
Try something like this:
if (typeof exports !== 'undefined') {
MyModels = exports;
Backbone.setDomLibrary(require('jquery'));
server = true;
} else {
MyModels = this.MyModels = {};
}
This will fail if you try to do any DOM functions though.

How to export properly in node.js?

I'm running some issues with my node application after upgrading to Express 3.0. So, since I'm rewriting it, I was trying to follow the style of routes/index.js because it looks clean.
So, in my main app.js I have some variables such as:
var calls = 0;
var agents = [];
And also use the node-mysql module. However, the routes definition doesn't have app.js scope but their own scope so calls and agents aren't visible.
How should I make them visible?
For mysql I tried something like:
// libraries/mysql.js
mysql = require('mysql');
var mysql_conf = {
host: myhost,
user: myuser,
password: mypass,
database: mydb
};
var mysql_client = mysql.createClient(mysql_conf);
exports.mysql_client;
//Later in routes/index.js
mysql_client = require('../helpers/mysql.js');
But it seems not to work as it says TypeError: Object #<Object> has no method 'query'
Any hints please?
The line
exports.mysql_client;
Does not actually assign anything to the mysql_client property of exports. It accesses a property, and immediately discards the value. You need to do something like this:
var mysql_client = mysql.createClient(mysql_conf);
exports.mysql_client = mysql_client;
// or simply
exports.mysql_client = mysql.createClient(mysql_conf);

Sharing constants between a web client and NodeJS server

I have a NodeJS application with common constants between the client and the server.
The constants are stored in variables rather than inline. Those variables could be defined in two separate files, one for client and one for server.
File 1:
// client_constants.js
MESSAGE_TYPE_A = "a";
MESSAGE_TYPE_B = "b";
File 2:
// server_constants.js
exports.MESSAGE_TYPE_A = "a";
exports.MESSAGE_TYPE_B = "b";
To avoid duplicate code I would like to store constants in a single location and a single format for both the client and the server. Wat do?
You can do something like this:
// constants.js
root = exports ? window
root.MESSAGE_TYPE_A = "a";
root.MESSAGE_TYPE_B = "b";
"exports" does not exist on the client side in which case it will use the default "window" object.
Sort of like Hector's answer, but works in my version of Node and in my browser because it uses comparisons to "undefined" and typeof.
var context = (typeof exports != "undefined") ? exports : window;
context.constant_name = "constant_name_string";

Resources