I have a JSON object stored in "aggRequest", this is my JSON
{
"state":{
"controllerStates":[
],
"rulesetStates":[
{
"rulesetContext":{
"dialog_stack":[
{
"dialog_node":"root"
}
],
"dialog_turn_counter":1,
"dialog_request_counter":1,
"_node_output_map":{
"node_1_1504607088493":[
0
]
},
"branch_exited":true,
"branch_exited_reason":"completed"
},
"convId":"XXXX",
"modelRef":"XXXX"
}
],
"selfCallCount":0,
"sysTurnCount":2,
"userTurnCount":2
},
"context":{
"conversationContext":{
"brand":"XXXX",
"channel":"XXX"
},
"userData":{
"tokens":[
]
}
},
"XXXXX":"2.0",
"XXXXX":"XXXXX",
"XXXXX":"XXXXX",
"XXXXX":"XXXXX",
"XXXXX":"XXXXX",
"turnStart":"2018-08-10T15:21:36.075Z",
"turnEnd":"2018-08-10T15:21:36.076Z"
}
I am trying to set the turnEnd to a new date using moment, the date is being created correctly, and I believe my function is correct:
const update = require('immutability-helper');
function updatePropertyValue() {
const momentDateChange = getRunDateAsString();
update(aggRequest, {
$set:
{
turnEnd: `${momentDateChange}`,
},
});
console.log(aggRequest)
}
When I view the addRequest console.log, the date has not been updated. Why would that be?
The whole idea of immutability is that you can not change the data. Instead you creating a new one. Like in the example below:
const update = require('immutability-helper');
const data = {
"turnEnd":"2018-08-10T15:21:36.076Z"
};
const newData = update(data, { $set: { turnEnd: 'voala!' } });
console.log(data); // it is immutable, right?
console.log(newData); // it is new version of the data
In order to solve the issue change your code to use updated (new version) of your data:
const updateAggRequest = update(aggRequest, { $set: { turnEnd: momentDateChange } });
Related
I have a nested array of objects like this:
var posts = [
{
_id:1234,
body:"text",
comments:[
{
_id:234,
body:"hello world", {
]
},
{
_id:434,
body:"hello world",
replies:[
{
_id:0e2345,
body:"hello",
{
]
}
]
}
]
I want to use normalizr to simplify array and use with redux. I have read the Normalizr documentation but it has few examples and I do not know what I am doing wrong.
I have tried the following code without success. The result I get is an array with undefined.
export function getPosts(state, action) {
const { payload } = action;
const { data} = payload;
const normalized = new schema.Entity("posts", {}, { idAttribute: "_id",});
const normalizedData = normalize(data, [normalized]);
return {
...state,
normalizedData,
};
}
I need something like this:
entities:{
posts:{
123:{
_id:123,
body:"hello world",
comments:{
234:{
_id:234,
body:"hello world",
replies:{
0e2345:{
_id:0e2345,
body:"oh no"
}
}
}
}
}
}
}
I've tried to reproduce this using a JSON object:
[{
"_id":1234,
"body":"text",
"comments":[
{
"_id":234,
"body":"hello world" }
]
},
{
"_id":434,
"body":"hello world",
"replies":[
{
"_id":0e2345,
"body":"hello"
}
]
}
]
And making the structure in the code:
import data from "./data.json";
import { normalize, schema } from "normalizr";
const _id = new schema.Entity('_id');
const body = new schema.Entity('body');
const normalized = new schema.Entity("posts", {
posts:{
_id:{
_id:_id,
body:body,
comments:{
_id:{
_id:_id,
body:body,
replies:{
_id:{
_id:_id,
body:body
}
}
}
}
}
}
});
const normalizedData = normalize(data, [normalized]);
console.log(normalizedData);
I get the next console output.
I have the data in one collection in the format given below
{
"tenantId":"Hello",
"timings":[{
"name":"Hi",
"category":"standard",
"pickAndPack":30,
"type":"pickup"
},
{
"name":"Hi",
"category":"premium",
"pickAndPack":30,
"type":"pickup"
}]
}
I want to update the data inside timings array.But its not working as expected.
Expected Output
{
"tenantId":"Hello",
"timings":[{
"name":"Hi",
"category":"standard",
"pickAndPack":30,
"type":"pickup"
},
{
"name":"Hello",
"category":"premium",
"pickAndPack":1200,
"type":"pickup"
}]
}
I have tried this query
let dataExists = [{
"name":"Hi",
"category":"standard",
"pickAndPack":30,
"type":"pickup"
},
{
"name":"Hello",
"category":"premium",
"pickAndPack":1200,
"type":"pickup"
}
masterdb.collection('corporate').update({ tenantId ,'timings.type': type}, { $set: { 'timings.$': dataExists } });```
<script>
export default {
data() {
return {
data: {},
dataTemp: {}
}
},
methods: {
updateData() {
let queries = { ...this.$route.query }
this.data = {
...this.data,
pID: queries.pid,
sID: queries.sid
}
this.dataTemp = {
...this.dataTemp,
pID: queries.pid,
sID: queries.sid
}
}
}
}
</script>
In the example above, when I update this.data, this.dataTemp will also be changed.
But I think it is not related to each other.
Please explain to me this problem.
Thanks!
This sets the initial values of data and dataTemp to empty objects. Note that they are both different objects though - data !== dataTemp.
data() {
return {
data: {},
dataTemp: {}
}
}
If someone calls this.updateData(), both are changed because we assign to both data and dataTemp. If you only want to update data, you could change the method to
updateData() {
let queries = { ...this.$route.query }
this.data = {
...this.data,
pID: queries.pid,
sID: queries.sid
}
}
I am using json-rule-engine .
https://www.npmjs.com/package/json-rules-engine
I am having a student list which have name and their percentage, Also I have business rule the percentage should be greater thank or equal to than 70 . so I want to print all students name those have percentage more than 70
here is my code
https://repl.it/repls/AlienatedLostEntropy#index.js
student list
const students = [
{
name:"naveen",
percentage:70
},
{
name:"rajat",
percentage:50
},
{
name:"ravi",
percentage:75
},
{
name:"kaushal",
percentage:64
},
{
name:"piush",
percentage:89
}
]
rule
engine.addRule({
conditions: {
all: [
{
fact: "percentage",
operator: "greaterThanInclusive",
value: 70
}
]
},
onSuccess(){
console.log('on success called')
},
onFailure(){
console.log('on failure called')
},
event: {
type: "message",
params: {
data: "hello-world!"
}
}
});
code
https://repl.it/repls/AlienatedLostEntropy#index.js
any update
The json-rules-engine module takes data in a different format. In your Repl.it you have not defined any facts.
Facts should be:
let facts = [
{
name:"naveen",
percentage:70
},
[...]
Also, the module itself doesn't seem to process an array of facts. You have to adapt it to achieve this. This can be done with:
facts.forEach((fact) => {
engine
.run(fact)
[...]
Finally, the student data is found inside the almanac. You can get these values with: results.almanac.factMap.get('[name|percentage|age|school|etc]').value
Here is the updated Repl.it: https://repl.it/#adelriosantiago/json-rules-example
I might have submitted a completely unrelated answer, but here goes. Since the students object is an array, you could just loop through it and then use an if else statement.
for (let i = 0; i < students.length; i++) {
if (students[i].percentage >= 70) {
console.log(students[i].name);
}
}
Sorry if this is incorrect!
Here is a working example.
Counting success and failed cases
const { Engine } = require("json-rules-engine");
let engine = new Engine();
const students = [
{
name:"naveen",
percentage:70
},
{
name:"rajat",
percentage:50
},
{
name:"ravi",
percentage:75
},
{
name:"kaushal",
percentage:64
},
{
name:"piush",
percentage:89
}
]
engine.addRule({
conditions: {
all: [{
fact: 'percentage',
operator: 'greaterThanInclusive',
value: 70
}]
},
event: { type: 'procedure_result'}
})
let result = {success_count : 0 , failed_count : 0}
engine.on('success', () => result.success_count++)
.on('failure', () => result.failed_count++)
const getResults = function(){
return new Promise((resolve, reject) => {
students.forEach(fact => {
return engine.run(fact)
.then(() => resolve())
})
})
}
getResults().then(() => console.log(result));
I have the following document:
{
"gameName":"Shooter",
"details":[
{
"submitted":1415215991387,
"author":"XYZ",
"subPlayer":{
"members":{
"squad1":[
{
"username":"John",
"deaths":0
}
]
},
"gameSlug":"0-shooter"
}
}
],
"userId":"foL9NpoZFq9AYmXyj",
"author":"Peter",
"submitted":1415215991608,
"lastModified":1415215991608,
"participants":[
"CXRR4sGf5AdvSjdgc",
"foL9NpoZFq9AYmXyj"
],
"slug":"1-shooterConv",
"_id":"p2QQ4TBwidjeZX6YS"
}
... and the following Meteor method:
Meteor.methods({
updateDeaths: function(gameSlug, user, squad) {
Stats.update({details.subPlayer.gameSlug: gameSlug}, ...}); // ???
}
});
My goal is to update the field deaths. The method has the argument user which is the user object (username = user.username). Furthermore, the argument squad is the squad name as string, e.g. squad1.
How can I do this?
Any help would be greatly appreciated.
You should use something like this:
db.collection.update(
{'userId':'foL9NpoZFq9AYmXyj', 'details.subPlayer.gameSlug':'0-shooter'},
{'$set':
{'details': [{
'subPlayer': {
'members': {
'squad1':[{'username':'John','death':1}]
}
}
}]
}
}
)
UPD:
Also, maybe a better way is to use find().snapshot().forEach(). But I don't know how Meteor supports it. Example:
Stats.find({'userId':'foL9NpoZFq9AYmXyj', 'details.subPlayer.gameSlug':'0-shooter' }).map(function (e) {
e.details.forEach(function (d) {
var squad = 'squad1';
d.subPlayer.members[squad].forEach(function (s) {
s.deaths = 10000;
});
});
Stats.update({'_id': e._id}, e);
});