Error trying to create a regex for Zapier - javascript

SOLVED: The error cause was the way I was declaring output var, please see the code
I am trying to create a regex to get the URL from a VSTS attachment in order to sync it with Jira. I tried to create a Run Javascript block with the following input:
Input Data
comment:
creationDate: 2017-10-11T11:31:19.293Z
lastModifiedDate: 2017-08-29T12:32:51.393Z
location: https://project.visualstudio.com/_apis/wit/attachments/ca6206de-0fab-451a-b0bc-89e70221dfcb
name: Capture.PNG
resourceId: 2255679
What I want to retrieve is the "location", so I thought that this would make the trick:
var regEx = new RegExp('location: (.*)', 'g'); // As mentioned in the comments, the 'g' part is not needed
var path = regEx.exec(inputData);
// output = path[1]; This was failing miserably
var output = [{'path': path[1], 'hello': 'world'}] //This works just fine, I guess the hello world part is not really needed :)
But I always get an error:
TypeError: Cannot read property '0' of null
I have tried to create a simple javascript test to run the same and it works just fine. Can you please help me?

Related

Chatkit: creating new user

I am following tutorial on here: https://docs.pusher.com/chatkit/quick_start/javascript
There is an example how to create user:
const chatkit = new Chatkit.default({
instanceLocator: "YOUR INSTANCE LOCATOR",
key: "YOUR SECRET KEY"
})
chatkit.createUser({
id: "bookercodes",
name: "Alex Booker"
})
However, when I am trying to run this code with my instanceLocator and key it gives me an error in the console:
SCRIPT445: Object doesn't support this action
Nore that I everything else Chatkit related works fine. I am just not able to create the new user.
Setting instanceLocator and key
You must replace the values of instanceLocator and key passed to
new Chatkit.default({
instanceLocator: "?",
key: "?"
})
To find the correct values, go to https://dash.pusher.com/chatkit and select the chatkit app you want to use.
On the next page, click the Credentisals tab as shown below:
Where you see <instanceLocator> in the screenshot you will find the string to use for instanceLocator, and where you see <key> you will find the string to use for key.
Creating user
If you want to create the user { id: "bookercodes", name: "Alex Booker" } as soon as you start the server, just to test that it works, then your code should work after you do as described above.
That's an IE11 error. Which browser are you using? Try Chrome.

Dots in snekfetch request are causing a problem

I'm trying to get data from a json file with snekfetch for my discord bot but I have a problem...
This is a part of the json file
"nodes":{
"main.c10":{
"online":1624,
"inbattles":829
},
If I want to get "online" number I should write this
var c10 = r.body.nodes.main.c10.online;
but with the dot after main is interpreting the request like this:
"nodes":{
"main {
.c10":{
"online":1624,
"inbattles":829
}
},
How I can solve this problem?
You can access that property with square brackets:
var c10 = r.body.nodes['main.c10'].online;
Edit in response to comments:
In this case r.body is a buffer, and so you need to first convert it to an object, only then you can access its properties like .nodes. You can find a working demo here.
JSON.parse(r.body.toString()).nodes['main.c10'].online

Handling JSON objects with UUID keys while templating with Handlebars.js?

I have a nested JSON object which looks like this
review:{
body:{
"1cfd0269-1b8f-418f-a7b2-45eb2fa7562b": "Text1",
"38d14dcd-6e70-46f9-8d81-9c5237cb7393": "Text2",
"01485828-39ef-4929-9e96-19758375eb9b": "Text3",
}
created_at: "2014-06-25T07:42:19Z",
id: "ea07aaa3-9db6-4868-b6f1-0887ef77f8ba",
product_id: "eb5a7c9c-c20d-4539-b04f-5a3fd8d26c87",
updated_at: "2014-06-25T07:42:19Z"
written_by: "09b3c6f1-cbcb-4544-8cc3-d073d17a8552",
written_on: "2014-06-25"
}
The JS is pretty straight-forward:
var context = {review:review};
html = template(context);
The template is largish but relevant part is here
<textarea class="review-body"> {{body["1cfd0269-1b8f-418f-a7b2-45eb2fa7562b"]}} </textarea>
I am getting a Parse error while trying to access attributes of body in an html template. Any ideas why this is happening?
Uncaught Error: Parse error on line 5:
...iew-edit-context">{{body["1cfd0269-1b8f-418f-a7b2-45eb2fa7562b"]}}</tex
-----------------------^
Expecting 'ID', 'DATA', got 'INVALID'
From the doc on Handlebars expressions :
To reference a property that is not a valid identifier, you can use segment-literal notation, [ :
{{#each articles.[10].[#comments]}}
{{/each}}
which means you have to use {{body.[1cfd0269-1b8f-418f-a7b2-45eb2fa7562b]}} in your template.
See http://jsfiddle.net/nikoshr/KVg9P/ for a demo.
yes that is work on webpage and angularJS expression retrieve it fine , but if you try to do the same in JavaScript it will return undefined error . for example if you try something like this
console.log(body.[1cfd0269-1b8f-418f-a7b2-45eb2fa7562b]);
or even
console.log(body['1cfd0269-1b8f-418f-a7b2-45eb2fa7562b']);
I do not the solution for it , but somewhere mention JavaScript is not like those mines sign on UUID , myself still looking for solution but temporary I am trying to remove the mine sign.

Mongoose, resolve path in embedded array

I have the following Mongoose schema:
var WeekSchema = new Schema({
days: [{
name: String
}]
});
and I want to get 'name' and do something with it (lets assume a validation).
So, I try to validate using the following code:
WeekSchema.path('days.name').validate(function(value){
return /monday|tuesday|wednesday|thursday|friday|saturday|sunday/i.test(value);
}, 'Invalid day');
but i get the error:
WeekSchema.path('days.name').validate(function(value){
^
TypeError: Cannot call method 'validate' of undefined
in fact if I print the resolved path with
console.log(WeekSchema.path('days.name'));
I have 'undefined'.
The question is, how can I get 'name' by using its path?
I met this issue today so I did a little research by printing out the main path, in your case: console.log(JSON.stringify(WeekSchema.path('days'), null, 4)); Then I figured out the path to the sub docs to be:
WeekSchema.path('days').schema.path('name');
This is my first answer here, hope it helps :)

Can't override property of an JavaScript object

I got a method that is formatting the date property of a message. A user has an array of messages.
user.messages[i].date = formatDate(user.messages[i].date);
// logs the correctly formatted date
console.log(formatDate(user.messages[i].date));
// logs the unformatted date
console.log(user.messages[i].date);
However, when I do it like the following code snippet it works.
user.messages[i] = {
name: user.messages[i].name,
body: user.messages[i].body,
_id: user.messages[i]._id,
date: formatDate(user.messages[i].date)
};
With the help of the comment from #plalx, I found a solution in this thread:
Stubbing virtual attributes of Mongoose model
This is a guess but you could add formatteddate:"" into your user model (or similar) and then try:
user.messages[i].formatteddate = formatDate(user.messages[i].date);
Therefore you are not re-configuring it.

Categories

Resources