Is it possible to access a node.js variable from the backend in a javascript file with pug?
It seems to work when using inline javascript inside the pug template, but I can't get it to work if when link to a .js file.
This is my router:
router.get("/test", function (req, res) {
testVar = {
title: "Test Title",
string: "Test string"
};
res.render("test", { testVar: testVar });
});
My template looks like this:
h1 #{testVar.title}
div #{testVar.string}
//- Inline JavaScript works
script.
var object = !{JSON.stringify(testVar)};
console.log(object.title);
console.log(object.string);
//- This does not work
script(src="/javascripts/test.js")
test.js have the same content as the inline javascript:
var object = !{JSON.stringify(testVar)};
console.log(object.title);
console.log(object.string);
If I try to run it with test.js enabled I get an error saying:
Uncaught SyntaxError: missing : after property id
the problem is with !{JSON.stringify(testVar)}
JSON.stringify turns your object to a string, then you put it in an object and turn it into a boolean.
So var object isn't an object and you're getting an error when referring to object.id.
You can test this by console.log(object) or even testVar.
Related
I have a mongo.js file in my react application that I want to set an environment variable to the correct database. I do this dynamically with the following code at the top of my file:
let url = '';
if (process.env.NODE_ENV === 'development')
url = process.env.REACT_APP_DATABASE;
else
url = process.env.MONGODB_URI;
Wen I try to start the node server I get this error TypeError: Parameter "url" must be a string, not undefined Why is it evaluating to undefined as opposed to the correct string?
Either process.env.REACT_APP_DATABASE or process.env.MONGODB_URI is being assigned and is undefined. Have you examined the values in a debugger?
The problem was due to an oversight in my package.json file. It looks like I had not installed dotenv. Oops.
I am running this command in my terminal
$mongo <database> -u user -p pw update.js
The update.js includes this code
var cursor = db.resources.find({
user: "dummy0"
});
print('cursor:', cursor);
var oldMatter = cursor.values.green;
My terminal output and the error I get is as follows
cursor: DBQuery: scrounge.resources -> { "user" : "dummy0" }
E QUERY TypeError: Cannot read property 'green' of undefined
What is the environment / API / driver I am using?
What is the correct Syntax to access the value?
Have you tried analyzing the cursor returned? db.resources.find will returns an object wrapped in an iterable cursor/array, so you can't just go about accessing properties of your object on the cursor variable - cursor.values.green will not work.
Try using findOne instead like so:
var obj = db.resources.findOne({
user: "dummy0"
});
This returns an object not wrapped as an iterable cursor/array so assuming it finds an object, you should be able to do
obj.values.green;
Trying to learn basic javascript, but ran into some issues.
Is it true that the following script stores the string to the parameter, turning it to a variable:
function funcOne(paraOne) {
document.write(paraOne);
}
funcOne("A Message");
Am I understanding it correctly, that this is equivalent to var paraOne = "A Message"; ?
Because when I try using this variable elsewhere, an error message just shows in console saying:
ReferenceError: Can't find variable: paraOne
Wondering what I am missing here.
The scope of a parameter is limited to the function that defines it. So, for example, this would be perfectly legal:
function funcOne(paraOne) {
document.write(paraOne);
console.log(paraOne);
}
funcOne("A Message");
While this would cause a RefernceError:
function funcOne(paraOne) {
document.write(paraOne);
}
funcOne("A Message");
console.log(paraOne); // Error here!
I am trying to grab the "David Welsh" text on this page: http://foster.uw.edu/faculty-research/directory/david-welsh/
When I do document.getElementById('Boundless').children[0].children[2].children[0].children[1].children[0].innerHTML in the browser console, I successfully select the text I want, but when I run my js file in Terminal, I get the error:
TypeError: 'null' is not an object (evaluating 'document.getElementById('Boundless').children')
Why is this working in my browser but not in my local js file?
casper.then(function(){
this.each(links,function(self,link){
self.thenOpen(link,function(a){
this.echo(this.getCurrentUrl());
var name = document.getElementById('Boundless').children[0].children[2].children[0].children[1].children[0].innerHTML;
casper.echo(name);
}
});
});
});
CasperJS is built on top of PhantomJS which has two contexts. The page context (casper.evaluate()) is sandboxed. It is the only way to access the DOM, you need to use it.
var name = casper.evaluate(function(){
return document.getElementById('Boundless').children[0].children[2].children[0].children[1].children[0].innerHTML;
});
casper.echo(name);
Everything that you want to use inside of it, you have to explicitly pass in. It has no access to variables defined outside.
The PhantomJS documentation also has something important to say:
Note: The arguments and the return value to the evaluate function must be a simple primitive object. The rule of thumb: if it can be serialized via JSON, then it is fine.
Closures, functions, DOM nodes, etc. will not work!
why is dtc not defined when I call #Html.Raw(Json.Encode(dtc))? I get an error that states "The name 'dtc' does not exist in the current context"
function createChartEncounterChart(chartid, dtc) {
var mdl = #Html.Raw(Json.Encode(dtc));
...
}
You cannot use a js variable in razor view code. The #Html.Raw(...) code is run on the server and dtc is a javascript variable that won't have any value (or be valid) until it's run in the browser.