markojs data variable access in client side javascript - marko

i am setting array of json objects into data variable like this data.journeyDetail = detail; in marko file i need to access it inside javascript block and for each record need to print FullName from the object. how to do this?
server side code:
data.journeyDetail = detail;
data.author = "najam";
this.body = marko.load("./views/journeyDetail.marko").stream(data);
marko file(client side)
<script>
console.log("author=$data.journeyDetail.length");
var length = $data.journeyDetail.length;
var recs = $data.journeyDetail;
console.log("len=", length);
console.log("array=", array);
for(var i=0; length; i++){
console.log("counter=", recs[i].origin);
}
</script>
when page is rendering its giving javascript error and line that has error have invalid assignment
var recs = [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object];
console.log("len=", length);

You'll want to serialize the template data to the page using JSON.stringify(...). This will allow the code within your script to access the data that was first available when the template was rendered. Keep in mind, that your data.journeyDetail data cannot have circular references because this will cause error to be thrown in JSON.stringify(...). You might want to use Array map function to transform the data in the array if you don't need all of the information when your script code runs.
You'll want to use:
<script>
console.log("author=$data.journeyDetail.length");
var length = $data.journeyDetail.length;
var recs = ${JSON.stringify(data.journeyDetail)};
console.log("len=", length);
console.log("array=", array);
for(var i=0; length; i++){
console.log("counter=", recs[i].origin);
}
</script>
The only change was this line:
var recs = ${JSON.stringify(data.journeyDetail)};

Related

Nodemailer Message Configuration Missing 'From'

So my nodemailer transport is working fine (for the most part) however, I am now trying to embed an image. The issue is now that I have the additional 'attachments' option in my mailOptions object, it is throwing the following error.
Error: 'from' parameter is missing
this is strange since I most definitely have a from parameter. Emails send fine without an attachment. Not sure what I am doing wrong.
var mailOptions = {
from: 'confirmation#donotreply.com',
to: email,
subject: 'Account verification',
html:'<p>Hello,\n\n' + 'Please verify your account by clicking the link' + confirmationLink + '\n\n' + '<img src="uniqueqr#qr.example" alt="something went wrong"/>',
attachments: [{
filename: 'qr.png',
path: '../path/to/my/file/qr.png',
cid: 'uniqueqr#qr.example'
}],
};
Try the following:
const mailOptions = Object.freeze({
from: 'confirmation#donotreply.com',
to: email,
subject: 'Account verification',
html: '<p>Hello</p>,\n\n' + 'Please verify your account by clicking the link' + confirmationLink + '\n\n' + '<img src="cid:uniqueqr#qr.example" alt="something went wrong"/>',
attachments: [{
filename: 'image.png',
path: '/path/to/file',
cid: 'uniqueqr#qr.example'
}]
});
note the cid inside img tag

Node Js prints [object Object] does not print session information

I am using the following Node Js code to use the Web service. But when I run this on Postman, I got [object Object] in console. But when I run on SoapUI, it will print the Session details
my code
router.post("/register", upload.single('image'), function (req, res, next)
{
var url = 'http://smeapps.mobitel.lk:8585/EnterpriseSMSV2/EnterpriseSMSWS?wsdl';
var session = {username: 'username', password: 'password'}
soap.createClient(url, function(err, client)
{
if(err)
console.log(err)
client.createSession(session, function(err, result)
{
console.log(result);
});
})
})
SoapUI session request
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://ws.esms.mobitel.lk/">
<soapenv:Header/>
<soapenv:Body>
<ws:createSession>
<!--Optional:-->
<arg0>
<!--Optional:-->
<customer>12</customer>
<!--Optional:-->
<id>12</id>
<!--Optional:-->
<password>password</password>
<!--Optional:-->
<username>username</username>
</arg0>
</ws:createSession>
</soapenv:Body>
</soapenv:Envelope>
SoapUI response
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://ws.esms.mobitel.lk/">
<SOAP-ENV:Body>
<ns1:createSessionResponse>
<return>
<expiryDate>2018-10-04T06:24:25+05:30</expiryDate>
<isActive>true</isActive>
<sessionId>761033662786</sessionId>
<user>0</user>
</return>
</ns1:createSessionResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
wrap the code into try catch
Error: SOAP-ENV:Server: Procedure 'createSession' not present
{"statusCode":500,"body":"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"><SOAP-ENV:Body><SOAP-ENV:Fault><faultcode>SOAP-ENV:Server</faultcode><faultstring>Procedure 'createSession' not present</faultstring></SOAP-ENV:Fault></SOAP-ENV:Body></SOAP-ENV:Envelope>\n","headers":{"date":"Thu, 04 Oct 2018 08:51:04 GMT","server":"Apache/2.2.15
(CentOS)","x-powered-by":"PHP/5.3.3","content-length":"304","connection":"close","content-type":"text/xml; charset=utf-8"},"request":{"uri":{"protocol":"http:","slashes":true,"auth":null,"host":"202.129.232.190:8585","port":"8585","hostname":"202.129.232.190","hash":null,"search":null,"query":null,"pathname":"/EnterpriseSMSV2/EnterpriseSMSWS.php","path":"/EnterpriseSMSV2/EnterpriseSMSWS.php","href":"http://202.129.232.190:8585/EnterpriseSMSV2/EnterpriseSMSWS.php"},"method":"POST","headers":{"User-Agent":"node-soap/0.8.0","Accept":"text/html,application/xhtml+xml,application/xml,text/xml;q=0.9,*/*;q=0.8","Accept-Encoding":"none","Accept-Charset":"utf-8","Connection":"close","Host":"202.129.232.190:8585","Content-Length":0,"Content-Type":"text/xml; charset=utf-8","SOAPAction":"\"\""}}}
Seems you are requesting REST API.
Response format for SOAP UI is XML. But for REST API is JSON. Then your are getting JSON format response. Thats why you are getting [object object] when you try to print that. So check out how to handle JSON.

How do I get the file name from nodejs app.post?

I want to get the file original name from this app.post (used with multer):
app.post('/', upload.array('file'), function(req, res){
console.log(req.files);
res.status(204).end();
});
Using console.log(req.files) I get:
[ { fieldname: 'file',
originalname: 'TSy16rd913.jpg',
encoding: '7bit',
mimetype: 'image/jpeg',
destination: './public/uploads/',
filename: 'TSy16rd913.jpg',
path: 'public/uploads/TSy16rd913.jpg',
size: 110736 } ]
Using console.log(req.files.originalname) or console.log(req.files.filename) gives undefined.
So how do I get originalname or filename?
As #Roland Starke answer, req.files is an array, so you have to do something like this
req.files[0].filename
To get all filenames :
req.files.forEach(function(value, key) {
console.log(value.filename)
})

Error while fetching json from database

I am able to send data to db using the following:
$("#set").click(function ()
{
var cells = graph.getCell();
var StateObject = graph.toJSON();
var SavedState = JSON.stringify(StateObject);
console.log("Here is the state object " + StateObject);
console.log("Here is the saved state: " + SavedState);
$.ajax({
url: 'JsonProcessor.do',
type: 'post',
dataType: 'json',
data: {
test: StateObject
}
});
});
But while fetching I get error in chrome console like this:
Uncaught Error: Graph JSON must contain cells array
Code to fetch data:
$("#get").click(function ()
{
$.ajax({
url: 'JsonProcessor.do',
type: 'get',
dataType: 'json',
success: function (data) {
var result = data;
console.log("Result from Database: " + result);
graph.fromJSON(result);
}
});
});
I can see the object fetched in chrome console like this: [object Object] and when I expand it
`0: Object
JSON_Diagram: "test=%5B%7B%22empID%22%3A%22%22%7D%2C%7B%22cells%22%3A%5B%7B%22type%22%3A%22basic.Rect%22%2C%22position%22%3A%7B%22x%22%3A-2%2C%22y%22%3A33%7D%2C%22size%22%3A%7B%22width%22%3A71%2C%22height%22%3A625%7D%2C%22angle%22%3A0%2C%22isInteractive%22%3Afalse%2C%22id%22%3A%22ca4cc8a8-7e95-43cb-a8d3-8cf24c82d43f%22%2C%22z%22%3A1%2C%22embeds%22%3A%5B%22b6aee295-16fa-41e7-b25e-d20610cbd631%22%2C%2288de9a15-cbc8-4c02-a358-30491d50cb37%22%5D%2C%22attrs%22%3A%7B%22rect%22%3A%7B%22fill%22%3A%22%23EEEEEE%22%2C%22stroke%22%3A%22%23008B8B%22%2C%22stroke-width%22%3A2%7D%2C%22.%22%3A%7B%22magnet%22%3Afalse%7D%7D%7D%2C%7B%22type%22%3A%22basic.Circle%22%2C%22size%22%3A%7B%22width%22%3A53%2C%22height%22%3A53%7D%2C%22position%22%3A%7B%22x%22%3A8%2C%22y%22%3A130%7D%2C%22angle%22%3A0%2C%22isInteractive%22%3Afalse%2C%22id%22%3A%22b145b765-a60b-4742-82a6-712da89e4dd0%22%2C%22z%22%3A3%2C%22attrs%22%3A%7B%22.%22%3A%7B%22magnet%22%3Afalse%7D%2C%22circle1%22%3A%7B%22fill%22%3A%22white%22%2C%22stroke-width%22%3A2%2C%22stroke%22%3A%22green%22%7D%7D%7D%2C%7B%22type%22%3A%22basic.Circle%22%2C%22size%22%3A%7B%22width%22%3A53%2C%22height%22%3A53%7D%2C%22position%22%3A%7B%22x%22%3A8%2C%22y%22%3A225%7D%2C%22angle%22%3A0%2C%22isInteractive%22%3Afalse%2C%22id%22%3A%221b99b595-57cd-4127-9ec0-d5842beb183a%22%2C%22z%22%3A4%2C%22attrs%22%3A%7B%22.%22%3A%7B%22magnet%22%3Afalse%7D%2C%22circle2%22%3A%7B%22fill%22%3A%22white%22%2C%22stroke%22%3A%22green%22%7D%7D%7D%2C%7B%22type%22%3A%22basic.Circle%22%2C%22size%22%3A%7B%22width%22%3A53%2C%22height%22%3A53%7D%2C%22position%22%3A%7B%22x%22%3A8%2C%22y%22%3A320%7D%2C%22angle%22%3A0%2C%22isInteractive%22%3Afalse%2C%22id%22%3A%227e953c11-d361-44d0-bf29-3d3fa146519c%22%2C%22z%22%3A5%2C%22attrs%22%3A%7B%22.%22%3A%7B%22magnet%22%3Afalse%7D%2C%22circle3%22%3A%7B%22fill%22%3A%22white%22%2C%22stroke%22%3A%22green%22%7D%7D%7D%2C%7B%22type%22%3A%22basic.Rect%22%2C%22position%22%3A%7B%22x%22%3A35%2C%22y%22%3A505%7D%2C%22size%22%3A%7B%22width%22%3A55%2C%22height%22%3A55%7D%2C%22angle%22%3A0%2C%22isInteractive%22%3Afalse%2C%22id%22%3A%22186a6dd7-2b5e-48d6-8e35-ee821ebbb4dc%22%2C%22z%22%3A7%2C%22attrs%22%3A%7B%22rect%22%3A%7B%22fill%22%3A%22%23FFED6B%22%2C%22stroke%22%3A%22%23DBCB62%22%2C%22width%22%3A55%2C%22height%22%3A55%2C%22stroke-width%22%3A1%2C%22transform%22%3A%22rotate(45)%22%7D%2C%22.%22%3A%7B%22magnet%22%3Afalse%7D%7D%7D%2C%7B%22type%22%3A%22basic.Rect%22%2C%22position%22%3A%7B%22x%22%3A10%2C%22y%22%3A50%7D%2C%22size%22%3A%7B%22width%22%3A51%2C%22height%22%3A41%7D%2C%22angle%22%3A0%2C%22isInteractive%22%3Afalse%2C%22id%22%3A%22b6aee295-16fa-41e7-b25e-d20610cbd631%22%2C%22z%22%3A9%2C%22parent%22%3A%22ca4cc8a8-7e95-43cb-a8d3-8cf24c82d43f%22%2C%22attrs%22%3A%7B%22rect%22%3A%7B%22fill%22%3A%22%23D6F2FC%22%2C%22stroke%22%3A%22%237E7E7E%22%7D%2C%22.%22%3A%7B%22magnet%22%3Afalse%7D%7D%7D%2C%7B%22type%22%3A%22basic.Rect%22%2C%22position%22%3A%7B%22x%22%3A10%2C%22y%22%3A420%7D%2C%22size%22%3A%7B%22width%22%3A51%2C%22height%22%3A41%7D%2C%22angle%22%3A0%2C%22isInteractive%22%3Afalse%2C%22id%22%3A%2288de9a15-cbc8-4c02-a358-30491d50cb37%22%2C%22z%22%3A12%2C%22parent%22%3A%22ca4cc8a8-7e95-43cb-a8d3-8cf24c82d43f%22%2C%22attrs%22%3A%7B%22.%22%3A%7B%22magnet%22%3Afalse%7D%2C%22rectGroup0%22%3A%7B%22fill%22%3A%22white%22%2C%22stroke%22%3A%22%237E7E7E%22%7D%7D%7D%2C%7B%22type%22%3A%22devs.Model%22%2C%22size%22%3A%7B%22width%22%3A751%2C%22height%22%3A170%7D%2C%22inPorts%22%3A%5B%5D%2C%22outPorts%22%3A%5B%5D%2C%22position%22%3A%7B%22x%22%3A160%2C%22y%22%3A123%7D%2C%22angle%22%3A0%2C%22id%22%3A%2237a88170-4da8-464d-99f9-32b72097c495%22%2C%22z%22%3A14%2C%22embeds%22%3A%5B%22e70977f4-52b0-4fdb-a852-8b3b63a5273e%22%5D%2C%22attrs%22%3A%7B%22.label%22%3A%7B%22text%22%3A%22CONTAINER%22%2C%22ref-y%22%3A0.1%2C%22y-alignment%22%3A%22middle%22%7D%2C%22rect%22%3A%7B%22fill%22%3A%22%22%2C%22opacity%22%3A%220.60%22%7D%7D%7D%2C%7B%22type%22%3A%22devs.Model%22%2C%22size%22%3A%7B%22width%22%3A51%2C%22height%22%3A41%7D%2C%22inPorts%22%3A%5B%22%22%5D%2C%22outPorts%22%3A%5B%22%22%5D%2C%22position%22%3A%7B%22x%22%3A234%2C%22y%22%3A187%7D%2C%22angle%22%3A0%2C%22id%22%3A%22e70977f4-52b0-4fdb-a852-8b3b63a5273e%22%2C%22z%22%3A15%2C%22parent%22%3A%2237a88170-4da8-464d-99f9-32b72097c495%22%2C%22attrs%22%3A%7B%22.%22%3A%7B%22magnet%22%3Atrue%7D%2C%22.port-body%22%3A%7B%22r%22%3A3%7D%2C%22.label%22%3A%7B%22text%22%3A%22%22%2C%22ref-x%22%3A0.4%2C%22ref-y%22%3A0.2%7D%2C%22rect%22%3A%7B%22fill%22%3A%22%23D6F2FC%22%2C%22stroke%22%3A%22%237E7E7E%22%7D%2C%22.inPorts+circle%22%3A%7B%22type%22%3A%22input%22%7D%2C%22.outPorts+circle%22%3A%7B%22type%22%3A%22output%22%7D%2C%22.inPorts%3E.port0%3E.port-label%22%3A%7B%22text%22%3A%22%22%7D%2C%22.inPorts%3E.port0%3E.port-body%22%3A%7B%22port%22%3A%7B%22id%22%3A%22in29%22%2C%22type%22%3A%22in%22%7D%7D%2C%22.inPorts%3E.port0%22%3A%7B%22ref%22%3A%22.body%22%2C%22ref-y%22%3A0.5%7D%2C%22.outPorts%3E.port0%3E.port-label%22%3A%7B%22text%22%3A%22%22%7D%2C%22.outPorts%3E.port0%3E.port-body%22%3A%7B%22port%22%3A%7B%22id%22%3A%22out30%22%2C%22type%22%3A%22out%22%7D%7D%2C%22.outPorts%3E.port0%22%3A%7B%22ref%22%3A%22.body%22%2C%22ref-y%22%3A0.5%2C%22ref-dx%22%3A0%7D%7D%7D%5D%7D%5D"
__proto__: Object
1: Object
2: Object
3: Object
length: 1
__proto__: Array[0]`
but problem is rendering the data on to view in jointjs
Please help if any body have already worked it out.
A key called JSON_Diagram is appended to json array when retrieving it. Json sent via ajax call must return exact json for the graph to render the diagram. You need to fix your JsonProcessor.do servlet to return same json. Hope this helps.
JointJS' fromJSON expects an input of the format { cells: [...] }. So I suspect that if you're saving your JSON using { test: JSON.stringify } you will need to JSON.parse(result.data) and then pass it to graph.fromJSON.
Final solution
graph.fromJSON(JSON.parse(result.data))

Json result with "undefined" prefixed

I use node https module to get auth information from another server, I get the result is "result=undefined{a:...,b:...}", so I can't use JSON.parse to parse the result data, but if I use "JSON.parse(body.substr(9))", I can get the right result.
For more information, if I use a post tool to fetch the result, I get the result type is "application/json" and the result is the right json object. I use the following code to fetch post result.
var options={
hostname:...,
port:null,
path:...,
method:'post',
rejectUnauthorized:false,
requestCert:true,
agent:false
}
var https.request(options,function(res){
var body;
res.on('data',function(chunk){
body+=chunk;
});
res.on('end',function(){
console.log(JSON.parse(body));
});
});
You should initialize body with an empty string:
var body = '';
because otherwise, the first time
body+=chunk;
is called, body is undefined and gets concatenated as the "undefined" string:
> var body;
undefined
> body += "{}"
'undefined{}'
> var body = '';
undefined
> body += "{}"
'{}'

Resources