Mongoose, resolve path in embedded array - javascript

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 :)

Related

Beacon prefill function

I already set up the Beacon 'identify' stuffs, and can open up using Beacon("open");
But when I try to use prefill function, i'm getting some error.
Beacon("prefill", {
name: "Steve Aoki",
email: "steve#aoki.com",
subject: "Need help with invoice",
text: "Hello, I need some help with my invoice. See attached.."
})
Error: Uncaught TypeError: Cannot read property 'filter' of undefined
Any code that I missed? Thanks in advance.
Reference here
You need to have a fields array, because in the example you posted, the one thing your code doesn't have is a fields array. This is also shown because filter is an array method, and calling fields.filter when you don't have fields will result in an undefined error.

Error trying to create a regex for Zapier

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?

Reading from JSON returns undefined

I'm trying to read from a JSON that contains my error messages, so that in case I'd ever want to change what my error messages say, I'd just change the JSON instead of diving into my source code. Everything seems to be working fine...
var fs = require('fs')
console.log("Now reading from error messages configuration file...");
var errorMsgs = JSON.parse(fs.readFileSync('config/error_msgs.JSON'));
console.log(errorMsgs)
This is what's in errorMsgs.json:
{
"bad_password" : [
{
"error" : "Incorrect login credentials.",
"details" : "This happens if you either have an incorrect username or password. Please try again with different credentials."
}
],
"missing_fields" : [
{
"error" : "Credentials failed to parse.",
"details" : "This happens when one or more fields are missing (or have illegal characters in them), please try again with different credentials."
}
]
}
When I log to the console, errorMsgs displays fine. When I log one of the items that errorMsgs has (like bad_password), it also works fine, as in it displays the items that are nested inside. However, when I attempt to retrieve a specific value like errorMsgs.bad_password['error'], it returns undefined. I can't seem to figure it out. I tried dot notation (errorMsgs.bad_password.error), which returns undefined. I tried the method above (errorMsgs.bad_password['error']) which also returns undefined. Asking for the typeof of errorMsgs, it returns object, which I assume makes it not a string. Passing the value to a variable first and then logging the variable doesn't do anything either. Is node converting it to a string on-the-fly, causing it to return undefined, or am I just doing something wrong?
bad_password" : [
{
"error" : "Incorrect login credentials.",
"details" : "This happens if you either have an incorrect username or password. Please try again with different credentials."
}
],
Your nested object is contained in an array.
errorMsgs.bad_password[0]['error']
This is what you're looking for. Just grab the first value of the array

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.

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