How to make {{shortcodes}} in node, express, jade app? - node.js

When I use something like #{user.name} in my input it spits out a string with "#{user.name}". What I would like to do is get data from my database by adding some {{shortcode}} like the image shows below. It could be any format, doesn't need to be in double curly braces. How can I solve this? Show examples or guide me in the right direction please.
Thank You.

you have to return the json object from your client side code called "user" having the property "name" then only you can able to populate the value into the front-end by using #{user.name}.

Related

Marklogic : "highlight" seem not work withe Node.js and QueryBuilder

I try to get an extract of the text with the searched words highlighted on a JSON collection.
My search syntax is:
qb.word(qb.field('doc_text'),vartxt)
With 'doc_text' declared as field
(field type: root, include root: false, includes: doc_text), in an Node.js application.
The search works well, and it is done well on this field ...
But in txt[0].results[kl].matches[0]['match-text'], I find the first 3 properties of the JSON,
and not an extract from 'doc_text' with the words found.
I have another application in which the highlights work correctly, but it is based on XML.
Did I forget something in the field declaration, or is the operation different between JSON
and XML data, or is the highlight system not running on JSON via Node.js and QueryBuilder ?
Kind regards
Fields do not work quite the same way in XML and JSON. I think you're running into this limitation:
http://docs.marklogic.com/guide/app-dev/json#id_24090
The value of a field in XML can be the concatenation of all the text nodes, but the same does not apply in JSON.
I think I understood !!!
This query gives a correct snippet, with the extract and the words highlighted :
mkcq.and(mkcq.collection('document'), mkcq.word(mkcq.field('doc_text'), 'connaitre'))
On the other hand, this query gives the first 3 fields of the JSON :
mkcq.and(mkcq.collection('document'), mkcq.word(mkcq.field('doc_text'), 'connaitre'), mkcq.value(mkcq.element('','doc_user'), 'mbp'))
I do not know if this is normal or not, but it should be able to be corrected either by a simplified query and a selection on the returned records, evening by a particular snippeter.
Kind regards

nunjucks not outputting all data from MongoDB

I am using node.js as a scripting language and I am also using nunjucks as the template engine. I have a weird situation going where some of the data that I retrieve from the MongoDB database is not being printed into document.
As you see hear these are the values that I want to print to the web page
But when you look at the web page, only some of the data has been printed out and the other information is missing.
I console.logged the data to prove that the values are in the database
A weird thing is that if you write the whole object into the code, like so
It will output all the data in one block. That includes the month, the year, and slug property that I am trying to output to the page. Yet, it only does that if I print out the whole object
I found out what was the problem. In my mongoose schema, I did not have the properties listed like slug, month, or date. So when I tried to retrieve data from those properties it did not output them. So If you are having similar problems make sure the properties are declared in your mongoose schema

ember display object value using handlebars markup

I'm new to ember and I would like to know how I can display the values stored in an object controller. I setup a bin and I'm now able to make it work:
http://jsbin.com/xehufetijo/edit?html,js,output
Any help would be much appreciated.
Thanks!
Try adding model in from of your attributes. For instance you would write:
{{model.first}} and {{model.last}}. If the attributes is attached to a controller, you don't need to specify it explicitly

The "right way" to load, crunch and pass variables from Express to Jade

I'm building an app in Express. In one of the views a logged in Superadmin is able to view all available clients/user. In this view I am loading a bunch of client data from my MongoDB/Mongoose with a simple:
app.get('/superadmin', function(req, res) {
Client.find({}, '_id company monthly_cost sms_cost', function (err, docs) ...
As you can see above i have choosen only the values that I need from the query. These four are: "_id", "company", "monthly_cost" and "sms_cost"
From the "_id" i can get a "creation date" by using .getTimestamp(), but the Dateobject this function return is bit to complex formated. I need a simpler date, something like: (YYYY-MM-DD). Im thinking of using a small node plugin like dateformat or possibly writing a very simple function that extract the YYYY, MM and DD from the IsoDate object and saving this in a new variable/array
Now to my questions:
Q1) WHERE is actually the right place for this code? I'm currently putting it inside the route handler above... consequently it will follow right below the code above. I'm thinking this is principally the right way according to a MVC pattern. I'm thinking I dont want to put this code in the Jade view template?
Q2) IN WHAT FORM should i save this data and HOW should i pass it along to Jade. Should I somehow add it to the "docs"-data... that is, the data I extract from my DB. Or should I rather put this creationDate in a separate array which i pass to jade side by side with the original DB-data.
I hope my questions are clear enough!
Q1:
If your Mongoose-query is solely dependent on your route /superadmin, this is exactly the right place to put your code. If you are using the same snippet in different routes or functions you might as well wrap it in a function that is accessible to every route in question. But don´t forget to also wrap req, res and other required variables. Have your calculations within your callback and use Jade only for representation of data.
Q2:
What do you mean by "save"? When you are already iterating over every document to do your calculations and transformations, create an extra field creationDate for every document and pass docs as a single parameter to the Jade file afterwards.

Jade renders unexpected values in templates for "populated" mongoose properties

I have a jade template where I'm printing out properties from a mongoose object. The object has properties that were populated via mongoose's dbref-like populate feature. When The template renders I get nonsense.
simplified example:
in the route
User.findById(req.params.user, function(err, user){
res.render('training', {training:user.training});
}).populate('training.details');
in the template:
a.training-link(href='/training/#{training.details.id}') ...
in the browser:
...
The first time the page is loaded, where I would expect a regular mongo hexadecimal string (4fac4e5f379cb0a68100015d) I get something like "O >°Cm5�". This only happens on the first page load after a server restart. All subsequent page loads render as expected.
What's happening here and how can I fix it?
Thanks.
This is a bit embarrassing but I figured it out.
There is a logical branch in our app that, for various reasons, causes the code in my question above to run on the second and subsequent loads but not on the first. The other branch, which only runs on the first load, makes a similar query but I hadn't added populate('training.details') to it. I believe the nonsense string O¬N_7°¦� is the result of calling .id on an ObjectId object rather than the populated training.details.
Looks like we need to refactor some code.

Resources