How do I pass the value of this var via the parameters? - javascript

In JQuery:
initSliders(socket, '!{request}');
request is an object (an HTTP request object in Express.js web app) that has such info as my session. When I pass this in to initSliders() it come in on the other side as the String literal "[object Object]". Why? I don't know. It behaves like an object before it passes in since commands like these execute:
console.log('!{request.session.user}');
console.log('!{request.session.date}');
console.log('!{request.session.city}');
Display things like
Alfred
11/13/2013
Manitoba
How can I keep it's structure when being passed into the function so I can reference it's properties like those above??
The request object is coming in through a render, via an app.js handler like this (which is why I use the single quotes and curly brackets around it, it's the only way I know how to reference this)
exports.dashboard = function(req, res){
res.render('dashboard', {request: req});
}

It prints out [object Object] because req.toString() returns it. res.render('dashboard', { request: JSON.stringify(req) }) also does not make much sense just because request is particularly huge object and you probably do not really want ALL the object properties being passed to render script. However, you can try... So it is highly recommended to pass only what is required as follows:
red.render('dashboard', { parameters : JSON.stringify({ param1 : req..., param2 : req.... and so on })

Related

How to send several arguments with ipcRenderer

I'd like to know how to send several arguments via ipcRenderer on an Electron app.
Should I send an array of arguments or just all the arguments separated by comma?
Thanks,
I would recommend an object for parameter transfer. In consequence, you can also think about implementing a consistent API for your application:
var _myreq = {
state: 0, //0 is no error, 4 is error with message, etc.
message: "", //can include error message (if any)
data: [0,4,6] //application data for request (String, Array, Object)
};
ipc.send('mychannel-functiona', _myreq);
Docs clearly shows that you can pass any number of argument to send.
Send a message to the main process asynchronously via channel, you can
also send arbitrary arguments. Arguments will be serialized in JSON
internally and hence no functions or prototype chain will be included.
From that point on you have no restrictions on how to use those arbitrary arguments. It depends on your needs, your codebase style etc.
We can pass many arguments for the ipcRenderer, you can refer this page: https://electronjs.org/docs/api/ipc-renderer.

req.flash() returning an empty object in app.js but works fine in a controller method

So i am setting my flash messages inside my controller method like this:req.flash('info', 'flash is working') and then i console.log(req.flash()) in the controller to see if it works. Then in my app.js file i simply pass it on to a res.locals so that it is available to me in my templates but there i was receiving an empty object so i did console inside the middleware to assign res.locals and it was empty there as well. Why is this happening ?
app.use((req, res, next) => {
res.locals.flashes = req.flash();
res.locals.h = helpers;
console.log(req.flash());
next();
});
btw i know the first request is supposed to return empty flash object but the second one returns empty as well
Alright, i made a stupid mistake with the order of requiring the session module and because of that sessions were not working properly and i was getting an empty object in response as the flash messages were not really getting stored in the session. Fixing the order of modules fixed it for me.

Passing String Parameter into My Socket.io-enabled Angular 2 Component

I am in the process of moving from using http to using socket.io to bring data into to my Angular 2 app. In the example I'd like help with I am using a private version of the api.service in my component constructor. All I need to do now is call the "get" function that's defined in the api.service (already set up to use socket.io). This is what the "get" function from the api.service looks like:
public get(req: string, options: any, callback: IRequestCallback) {
// go get stuff
}
Now in my component I had been using an http get function that looked like this:
getByCategory() {
return this._http.get(this._url)
.map((response:Response) => response.json())
.catch(this._errorsHandler);
}
Now I am change this to use the socket.io function from my api.service instead. As you can see in my code at the top, the socket "get" call from the api.service is asking for 3 parameters (a string, options, and a callback). I tried this:
getByCategory() {
return this.__socket.get(this._url, this.args, function(data) {
});
}
... but now I realize the URL is already being provided in the api.service. So what's stumping me, as silly as this may sound, is what I can pass as the first parameter. I tried passing in an empty string as the first parameter, but that errored out. What string could I pass here, instead of the url? Or is there some other way around this?
First, I've had no issues with the Http module, so I'm curious why you made the switch (not that I know all of the use cases!)
That being said, it sounds like it really depends on how socket.io is handling the underlying code -- optional parameters in Typescript ("optional") normally expect undefined as a placeholder, so you could try passing undefined in this case.

Backbone JS collection parse

Hi , I have some issue with parse method!!!!!
as you can see in backbone js document the parse method in collection has this syntax :
collection.parse(response, options)
1) I want to know why we should use / override the parse method and what is the main usage of that?
2) I read some article and I get that the parse method give us the data structure for the client-side.
3 ) I really have issue for understanding the arguments of parse method .
- what is the options for??
can you give me an example of using parse method with two parameters?
Thanks!
The docs have a nice summary:
parse is called by Backbone whenever a collection's models are returned by the server, in fetch. The function is passed the raw response object, and should return the array of model attributes to be added to the collection. The default implementation is a no-op, simply passing through the JSON response.
http://backbonejs.org/#Collection-parse
1) You should return an array of model attributes. If your JSON response only has this then you don't need to do anything. Typically the parse override is used simply to point inside the JSON object at the right part. For example if your response was like this:
{
httpCode: 200,
responseMessage: 'success',
data: [ {model1}, {model2} ...]
}
Then you would need to override parse to point to the data key:
parse: function(response) {
return response.data;
}
2) They meant that the response arg is the object which was returned by the server.
3) The second options arg is the options that was passed to the .fetch call. You don't need to worry about it unless you want to do some specific logic based on the URL, the HTTP method or anything else that can be passed to fetch (and jQuery.ajax options and some Backbone ones like reset).
4)
parse: function(response, options) {
// For some reason POST requests return a different data structure.
if (options.method === 'POST') {
return response.data;
}
return response;
}

Backbone.js attribute / fetching seems to work funny

So, I have the following code that access my rest api:
Employees.Employee = Backbone.Model.extend({
urlRoot: 'api/employee'
})
var daryl = new Employees.Employee({id:17})
daryl.fetch()
console.log(daryl.attributes)
Now, when I console.log the attributes, the daryl object is set up like this roughly:
daryl = {
attributes:
[0]: {
id: 17,
first: 'Daryl',
last: 'xxxx',
email: 'xxx'
},
id: 17,
watchers...
protos...
}
So trying to daryl.get('first') results in undefined. Everything else is stored in the object in the array at index 0. Why is this? I'm a newbie here but this is definitely not how most of the tutorials seem to show how backbone works.
So if I do daryl.get('first'), I get undefined. daryl.get('id') works as expected. daryl.get('0') actually returns a plain old javascript object of the actual model, i.e. what I would probably expect to be my backbone model to ACTUALLY be. why is this?
Your server appears to be returning an array in its response, hence why calling model.get('0') is returning the attributes you really wanted. You either need to modify the server's response to only return the object (instead of an object inside an array) or you need to add a parse method to your model to return the first item in the response array.
Not sure if this is the issue in question (but doing console.log after calling fetch is problematic), but it is important to keep in mind that daryl.fetch()is happening asynchronously.
That is to say, you should try:
daryl.fetch().done(function(){
console.log(daryl.attributes);
model.get("first");
});
or
daryl.fetch({success : function(model){
console.log(model);
model.get("first");
}});
This ensures that the AJAX request was complete prior to trying to act on the model and very well maybe why get returns undefined.

Categories