Mongoose ValidationError Path required that is out of subdocument array bounds - node.js

This error is super wacky. It doesn't always fail, but when it does it looks like this.
I have some code that changes the "code" (a one character string) of elements in a subdocument array. It goes through each goal, checks to see if there's a change to be applied, and if so, applies it.
for (i = 0; i < user.goals.length; i++) {
if (transformsMap[user.goals[i].code]) {
user.goals[i].code = transformsMap[user.goals[i].code]
}
}
user.goals.sort(function (a,b) {return a.code.charCodeAt(0) - b.code.charCodeAt(0))
When I save it, sometimes I get an error like this:
'goals.3.code':
{ [ValidatorError: Path `code` is required.]
...but 3 in this case, is the length of the goals array. ie there is no goals.3 subdocument. I've tried logging user.goals and user.goals.length right before validation and they both agree that there are only 3 elements in the array.
I am totally baffled.

How about adding more checks? And also setting a default value if the if clause fails?
for (i = 0; i < user.goals.length; i++) {
if (user.goals[i] && user.goals[i].code && transformsMap[user.goals[i].code]){
user.goals[i].code = transformsMap[user.goals[i].code]
} else {
user.goals[i].code = "" // Whatever this is <-- default value.
}
}
user.goals.sort(function (a,b) {return a.code.charCodeAt(0) - b.code.charCodeAt(0))

Related

Register array with a field in only one of the registers in DML 1.4

I want to make a register array, where one of the registers should include a field in bit 0 with a value of 1.
I have tried using a conditional without any success.
register feature_bits[i < N_FEATURE_SELECT_REGS] size 4 is unmapped {
#if (i == 1) {
field virtio_f_version_1 # [0] "VIRTIO_F_VERSION_1" {
param init_val = 1;
}
}
}
I have also tried indexing the register element and set the field accordingly
register feature_bits[i < N_FEATURE_SELECT_REGS] size 4 is unmapped;
register feature_bits[1] {
field VIRTIO_F_VERSION_1 # [0] {
param init_val = 1;
}
}
None of these approaches worked.
If we take a step back and look at what you're trying to accomplish here, it's possible that a third option can be usable. If it's only the init-value of some fields that differs, you can create a template type and iterate over it like this:
template feature_bit {
param lsb : uint64;
}
bank regs is (init, hard_reset) {
register features[i < 4] size 4 # unmapped;
method init() {
foreach f in (each feature_bit in (this)) {
features[f.lsb / 32].val[f.lsb % 32] = 1;
}
}
method hard_reset() {
init();
}
group featA is feature_bit { param lsb = 42; }
group featB is feature_bit { param lsb = 3; }
}
The init-template is depth-first recursive, so the feature registers will be initialized first, and then the init() method of the bank will run and set the value of the feature-registers by iterating over all instances of the feature_bit template. We also call init from hard_reset, which is also depth-first recursive, otherwise the register will be 0 after reset.
Arrays in DML are homogeneous; every subobject must exist for all indices. This is because when you write a method inside an array, each array index translates to an implicit method argument, so in your case, if a method in your register calls this.virtio_f_version.read(), this translates to something like regs__feature_bits__virtio_f_version_1__read(_dev, _i). This function exists for all values of i, therefore the field must exist for all values of i.
There are two approaches to solve your problem. The first is to let the field exist in all indices, and add code to make it pretend to not exist in indices other than 1:
register feature_bits[...]; {
field VIRTIO_F_VERSION_1 # [0] {
param init_val = i == 1 ? 1 : 0;
method write(uint64 value) {
if (i == 1) {
return set_f_version_1(value);
} else if (value != this.val) {
log spec_viol: "write outside fields";
}
}
}
}
The second approach is to accept that your set of registers is heterogeneous, and use a template to share code between them instead:
template feature_bit {
param i;
#if (i == 1) {
field virtio_f_version ... { ... }
}
... lots of common fields here ...
}
register feature_bits_0 is feature_bit { param index = 0; }
register feature_bits_1 is feature_bit { param index = 1; }
register feature_bits_2 is feature_bit { param index = 2; }
...
Which approach to choose is a trade-off; the first solution is more economic in terms of memory use and compile speed (because DML doesn't need to create almost-identical copies of methods across register indices), whereas the second solution gives a model that more accurately reflects the specification upon inspection (because the model will declare to the simulator that virtio_f_version_1 is a field only for one of the registers).
If you read the spec and you get the feeling that this field is a singular exception to an otherwise homogeneous array, then the first approach is probably better, but if registers vary wildly across indices then the second approach probably makes more sense.

Why do I need to test for the existence of a property on an object before incrementing it, when counting the number of letters in an object?

I came across a youtube video showing how to count each letters' occurrence using Javascript, eg when the input is "hello", the function will return
{h:1 e:1 l:2 o:1}
Like this:
const obj = {};
for (let i = 0; i < wordInput.length; i++) {
const char = wordInput[i];
if (!obj[char]) {
obj[char] = 0;
}
obj[char]++;
}
Why do we need the if statement? He said something like "some of these are undefined", but I'm not really sure what that means, can you explain why we need the if (!obj[char])?
In the first run of the loop
i = 0
-----------
char = 'h'
obj = {}
obj[char] ==> obj['h'] ==> undefined
So if you try to do
obj[char]++ ==> obj['h']++ , it will throw error as the value is undefined. Instead initialize it to 0 first, using the if statement, then increment it.
The obj object starts out empty. Trying to access any property of it will return undefined, and if you try to use ++ on undefined, you'll get NaN (Not a Number), which is not what you want:
const obj = {};
obj.foo++;
console.log(obj.foo);
To fix that, before incrementing, check to see if the property exists first, and if it doesn't, set it to 0. 0 is incrementable; undefined is not.
const obj = {};
if (!obj.foo) {
obj.foo = 0;
}
obj.foo++;
console.log(obj.foo);
The code in your question does the same sort of thing, except it iterates over chars (characters of the wordInput string). If the character hasn't been put as a property of the object yet, it must be set before incrementing, otherwise the resulting object will contain useless NaN values:
const wordInput = 'foo bar';
const obj = {};
for (let i = 0; i < wordInput.length; i++) {
const char = wordInput[i];
obj[char]++;
}
console.log(obj);
The if statement is for the first entry of a character. Before 'a' comes, there's no property in obj called 'a'. So obj[char] defines a property and sets its value to 0. Before that, there's no property, so it's undefined. You can't apply operator to undefined.
This is how obj looks like when the first letter comes in 'hello'
before the if statement:
obj = {};
so obj['h'] = undefined;
It enters the if statement, then:
obj = { 'h' : 0 }

If statements not working with JSON array

I have a JSON file of 2 discord client IDs `{
{
"premium": [
"a random string of numbers that is a client id",
"a random string of numbers that is a client id"
]
}
I have tried to access these client IDs to do things in the program using a for loop + if statement:
for(i in premium.premium){
if(premium.premium[i] === msg.author.id){
//do some stuff
}else{
//do some stuff
When the program is ran, it runs the for loop and goes to the else first and runs the code in there (not supposed to happen), then runs the code in the if twice. But there are only 2 client IDs and the for loop has ran 3 times, and the first time it runs it goes instantly to the else even though the person who sent the message has their client ID in the JSON file.
How can I fix this? Any help is greatly appreciated.
You may want to add a return statement within your for loop. Otherwise, the loop will continue running until a condition has been met, or it has nothing else to loop over. See the documentation on for loops here.
For example, here it is without return statements:
const json = {
"premium": [
"aaa-1",
"bbb-1"
]
}
for (i in json.premium) {
if (json.premium[i] === "aaa-1") {
console.log("this is aaa-1!!!!")
} else {
console.log("this is not what you're looking for-1...")
}
}
And here it is with return statements:
const json = {
"premium": [
"aaa-2",
"bbb-2"
]
}
function loopOverJson() {
for (i in json.premium) {
if (json.premium[i] === "aaa-2") {
console.log("this is aaa-2!!!!")
return
} else {
console.log("this is not what you're looking for-2...")
return
}
}
}
loopOverJson()
Note: without wrapping the above in a function, the console will show: "Syntax Error: Illegal return statement."
for(i in premium.premium){
if(premium.premium[i] === msg.author.id){
//do some stuff
} else{
//do some stuff
}
}
1) It will loop through all your premium.premium entries. If there are 3 entries it will execute three times. You could use a break statement if you want to exit the loop once a match is found.
2) You should check the type of your msg.author.id. Since you are using the strict comparison operator === it will evaluate to false if your msg.author.id is an integer since you are comparing to a string (based on your provided json).
Use implicit casting: if (premium.premium[i] == msg.author.id)
Use explicit casting: if (premium.premium[i] === String(msg.author.id))
The really fun and easy way to solve problems like this is to use the built-in Array methods like map, reduce or filter. Then you don't have to worry about your iterator values.
eg.
const doSomethingAuthorRelated = (el) => console.log(el, 'whoohoo!');
const authors = premiums
.filter((el) => el === msg.author.id)
.map(doSomethingAuthorRelated);
As John Lonowski points out in the comment link, using for ... in for JavaScript arrays is not reliable, because its designed to iterate over Object properties, so you can't be really sure what its iterating on, unless you've clearly defined the data and are working in an environment where you know no other library has mucked with the Array object.

not equal to not working in postgresql query

if (req.body.positionDetails && req.body.positionDetails.length > 0) {
let total = req.body.positionDetails.length;
for (let i = 0; i < total; i++) {
db.query('SELECT * FROM position WHERE position <> $1',[req.body.positionDetails[i].position],function(err,p) {
console.log(p.rows)
});
}
}
It is selecting all the values from the database without checking the condition. How to solve this??
data is like
"positionDetails":[{"position":"manager"},{"position":"developer"}] and it is from postman.
Your prepared statement looks off to me. Try using ? as a placeholder for the position in your query.
db.query(
'SELECT * FROM position WHERE position <> ?', [req.body.positionDetails[i].position],
function(err, p) {
console.log(p.rows)
});
If this fixes the problem, then I suggest that you were comparing the position against the literal string $1, and every comparison failed resulting in every record appearing in the result set.

Map/Reduce differences between Couchbase & CloudAnt

I've been playing around with Couchbase Server and now just tried replicating my local db to Cloudant, but am getting conflicting results for my map/reduce function pair to build a set of unique tags with their associated projects...
// map.js
function(doc) {
if (doc.tags) {
for(var t in doc.tags) {
emit(doc.tags[t], doc._id);
}
}
}
// reduce.js
function(key,values,rereduce) {
if (!rereduce) {
var res=[];
for(var v in values) {
res.push(values[v]);
}
return res;
} else {
return values.length;
}
}
In Cloudbase server this returns JSON like:
{"rows":[
{"key":"3d","value":["project1","project3","project8","project10"]},
{"key":"agents","value":["project2"]},
{"key":"fabrication","value":["project3","project5"]}
]}
That's exactly what I wanted & expected. However, the same query on the Cloudant replica, returns this:
{"rows":[
{"key":"3d","value":4},
{"key":"agents","value":1},
{"key":"fabrication","value":2}
]}
So it somehow only returns the length of the value array... Highly confusing & am grateful for any insights by some M&R ninjas... ;)
It looks like this is exactly the behavior you would expect given your reduce function. The key part is this:
else {
return values.length;
}
In Cloudant, rereduce is always called (since the reduce needs to span over multiple shards.) In this case, rereduce calls values.length, which will only return the length of the array.
I prefer to reduce/re-reduce implicitly rather than depending on the rereduce parameter.
function(doc) { // map
if (doc.tags) {
for(var t in doc.tags) {
emit(doc.tags[t], {id:doc._id, tag:doc.tags[t]});
}
}
}
Then reduce checks whether it is accumulating document ids from the identical tag, or whether it is just counting different tags.
function(keys, vals, rereduce) {
var initial_tag = vals[0].tag;
return vals.reduce(function(state, val) {
if(initial_tag && val.tag === initial_tag) {
// Accumulate ids which produced this tag.
var ids = state.ids;
if(!ids)
ids = [ state.id ]; // Build initial list from the state's id.
return { tag: val.tag,
, ids: ids.concat([val.id])
};
} else {
var state_count = state.ids ? state.ids.length : state;
var val_count = val.ids ? val.ids.length : val;
return state_count + val_count;
}
})
}
(I didn't test this code, but you get the idea. As long as the tag value is the same, it doesn't matter whether it's a reduce or rereduce. Once different tags start reducing together, it detects that because the tag value will change. So at that point just start accumulating.
I have used this trick before, although IMO it's rarely worth it.
Also in your specific case, this is a dangerous reduce function. You are building a wide list to see all the docs that have a tag. CouchDB likes tall lists, not fat lists. If you want to see all the docs that have a tag, you could map them.
for(var a = 0; a < doc.tags.length; a++) {
emit(doc.tags[a], doc._id);
}
Now you can query /db/_design/app/_view/docs_by_tag?key="3d" and you should get
{"total_rows":287,"offset":30,"rows":[
{"id":"project1","key":"3d","value":"project1"}
{"id":"project3","key":"3d","value":"project3"}
{"id":"project8","key":"3d","value":"project8"}
{"id":"project10","key":"3d","value":"project10"}
]}

Resources