Write Array in nodeOpcua - node.js

Hiii, some body know how i can write Uint16Array in kepserver i got some error:
ConstantStatusCode {
_value: 2147483648,
_description: 'The value is bad but no specific reason is known.',
_name: 'Bad' } ]
i'm try this:
var valor = new Uint16Array([ 2, 23, 23, 12, 24, 3, 25, 3, 26, 3, 27, 3, 28, 1, 43690, 1, 1261, 0, 0, 0, 0, 0, 0, 0, 65535, 11 ])
nodeToWrite[0] = {
nodeId: resolveNodeId("ns=2;s=" + endereco[0].ADDRESS),
attributeId: opcua.AttributeIds.Value,
value: /new DataValue(/{ value: {/ Variant /
dataType: 5,
arrayType: 1,
value: valor,
}
}
}

const {AttributeIds, OPCUAClient, DataType, VariantArrayType} = require("node-opcua");
const endpointUrl = "opc.tcp://localhost:48010";
(async () => {
const client = OPCUAClient.create({ endpoint_must_exist: false});
await client.withSessionAsync(endpointUrl, async (session) => {
const arrayOfvalues = new Uint16Array([ 2, 23, 23, 12, 24, 3, 25, 3, 26, 3, 27, 3, 28, 1, 43690, 1, 1261, 0, 0, 0, 0, 0, 0, 0, 65535, 11 ]);
const nodeToWrite = {
nodeId: "ns=2;s=Demo.Static.Arrays.UInt16",
attributeId: AttributeIds.Value,
value: {
value: {
dataType: DataType.UInt16,
arrayType: VariantArrayType.Array,
value: arrayOfvalues,
}
}
}
const statusCode = await session.write(nodeToWrite);
console.log("write statusCode = ",statusCode.toString());
});
})();
As demonstrated above, writing a Array of Uint16 is ok when addressing node ns=2;s=Demo.Static.Arrays.UInt16 of UAServerCPP from Unified Automation.
I would contact Kepware for support.

Related

How do I query a set of objects with an array of values in mongoose?

I have a schema like this
const rankSchema = new Schema(
{
rank: { type: Object, default: {} },
lastUpdated: { type: Date, default: Date.now() },
},
{ minimize: false }
);
And my database has an object 'rank' with many other objects inside of it like this.
rank: {
Person1: { Stat1: 2, Stat2: 0, Stat3: 0, Stat4: 2, Stat5: 4 },
Person2: { Stat1: 4, Stat2: 0, Stat3: 0, Stat4: 2, Stat5: 2 },
Person3: { Stat1: 1, Stat2: 0, Stat3: 0, Stat4: 2, Stat5: 1 },
Person4: { Stat1: 2, Stat2: 0, Stat3: 0, Stat4: 2, Stat5: 3 }
}
Now I have an array of strings that contains a few of these people
['Person1', 'Person2']
I want to be able to find all the person objects in that array and return their stats.
So essentially the final output after using the array of strings would be
Person1: { Stat1: 2, Stat2: 0, Stat3: 0, Stat4: 2, Stat5: 4 },
Person2: { Stat1: 4, Stat2: 0, Stat3: 0, Stat4: 2, Stat5: 2 }
I tried using $in and various different queries but nothing seems to work and I am stumped.
Thanks
You could use a combination of $objectToArray and $arrayToObject to filter your object by dynamic field names but if your parameters are known when you're building your query then it's easier to use regular .find() and apply projection:
db.collection.find({},{ "rank.Person1": 1, "rank.Person2": 1})
let input = ['Person1', 'Person2'];
let entries = input.map(p => ([`rank.${p}`, 1]))
let projection = Object.fromEntries(entries);
console.log(projection);
Mongo Playground

Why Express api return two objects with same data?

I'm creating an API with Express and SQL Server as DB. I've created a post method and it works fine, but i'm having problems with the get method, 'cause it's returning two objects with the same data. This is my code:
const express = require('express');
const bodyParser = require('body-parser');
const sql = require('mssql');
const app = express();
app.use(bodyParser.json());
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, contentType,Content-Type, Accept, Authorization");
next();
});
const dbConfig = {
user: "theUser",
password: "thePass",
server: "theServer",
database: "theDB"
}
const executeQuery = function (res, query) {
sql.connect(dbConfig, function (err) {
if (err) {
console.log(err);
res.send(err);
}
else {
// create Request object
var request = new sql.Request();
// query to the database
request.query(query, function (err, result) {
if (err) {
console.log(err);
res.send(err);
}
else {
res.send(result);
sql.close();
}
});
}
});
}
//Get All
app.get("/api/HolidayBaseApi", function (req, res) {
var query = "SELECT * FROM [HolidaysBase]";
executeQuery(res, query);
//executeQuery(res, query);
});
app.post("/api/HolidayBaseApi", function (req, res) {
var query = "INSERT INTO [HolidaysBase] (EmployeeNumber, PeriodBegin, PeriodEnd, WorkedYears, DaysPerYear, TakenDays, RemainingDays) VALUES ('"+req.body.EmployeeNumber+"','"+req.body.PeriodBegin+"','"+req.body.PeriodEnd+"','"+req.body.WorkedYears+"','"+req.body.DaysPerYear+"','"+req.body.TakenDays+"','"+req.body.RemainingDays+"')";
executeQuery(res, query);
});
const PORT = process.env.PORT || 8080
app.listen(PORT, () => {
console.log("App now running on port", PORT);
});
I'm testing on postman and i have the next return:
{
"recordsets": [
[
{
"Id": 1,
"EmployeeNumber": 4,
"PeriodBegin": "2018-04-01T00:00:00.000Z",
"PeriodEnd": "2019-03-31T00:00:00.000Z",
"WorkedYears": 6,
"DaysPerYear": 18,
"TakenDays": 10,
"RemainingDays": 8
},
{
"Id": 2,
"EmployeeNumber": 5,
"PeriodBegin": "2018-08-02T00:00:00.000Z",
"PeriodEnd": "2018-07-31T00:00:00.000Z",
"WorkedYears": 6,
"DaysPerYear": 18,
"TakenDays": 9,
"RemainingDays": 9
},
{
"Id": 5,
"EmployeeNumber": 9,
"PeriodBegin": "2018-10-15T00:00:00.000Z",
"PeriodEnd": "2019-10-15T00:00:00.000Z",
"WorkedYears": 6,
"DaysPerYear": 18,
"TakenDays": 0,
"RemainingDays": 18
}
]
],
"recordset": [
{
"Id": 1,
"EmployeeNumber": 4,
"PeriodBegin": "2018-04-01T00:00:00.000Z",
"PeriodEnd": "2019-03-31T00:00:00.000Z",
"WorkedYears": 6,
"DaysPerYear": 18,
"TakenDays": 10,
"RemainingDays": 8
},
{
"Id": 2,
"EmployeeNumber": 5,
"PeriodBegin": "2018-08-02T00:00:00.000Z",
"PeriodEnd": "2018-07-31T00:00:00.000Z",
"WorkedYears": 6,
"DaysPerYear": 18,
"TakenDays": 9,
"RemainingDays": 9
},
{
"Id": 5,
"EmployeeNumber": 9,
"PeriodBegin": "2018-10-15T00:00:00.000Z",
"PeriodEnd": "2019-10-15T00:00:00.000Z",
"WorkedYears": 6,
"DaysPerYear": 18,
"TakenDays": 0,
"RemainingDays": 18
}
],
"output": {},
"rowsAffected": [
3
]
}
As you can see, it's returning two times the same object. Someone knows why it's returning two recordset and how can i fix it? I've been searching since yesterday, but there's no info of this behavior.
I'm using Express.js, Node and SQL Server.
I've found the solution by myself. On my executeQuery just need to get inside of the recordsets in the result:
const executeQuery = function (res, query) {
sql.connect(dbConfig, function (err) {
if (err) {
console.log(err);
res.send(err);
}
else {
// create Request object
var request = new sql.Request();
// query to the database
request.query(query, function (err, result) {
if (err) {
console.log(err);
res.send(err);
}
else {
res.send(result.recordsets);
sql.close();
}
});
}
});
}
Doing this, i have kinda object inside of an object as a response:
[
[
{
"Id": 1,
"EmployeeNumber": 4,
"PeriodBegin": "2018-04-01T00:00:00.000Z",
"PeriodEnd": "2019-03-31T00:00:00.000Z",
"WorkedYears": 6,
"DaysPerYear": 18,
"TakenDays": 10,
"RemainingDays": 8
},
{
"Id": 2,
"EmployeeNumber": 5,
"PeriodBegin": "2018-08-02T00:00:00.000Z",
"PeriodEnd": "2018-07-31T00:00:00.000Z",
"WorkedYears": 6,
"DaysPerYear": 18,
"TakenDays": 9,
"RemainingDays": 9
},
{
"Id": 5,
"EmployeeNumber": 9,
"PeriodBegin": "2018-10-15T00:00:00.000Z",
"PeriodEnd": "2019-10-15T00:00:00.000Z",
"WorkedYears": 6,
"DaysPerYear": 18,
"TakenDays": 0,
"RemainingDays": 18
},
{
"Id": 6,
"EmployeeNumber": 37,
"PeriodBegin": "2018-07-27T00:00:00.000Z",
"PeriodEnd": "2019-07-27T00:00:00.000Z",
"WorkedYears": 4,
"DaysPerYear": 16,
"TakenDays": 3,
"RemainingDays": 13
},
{
"Id": 7,
"EmployeeNumber": 77,
"PeriodBegin": "2018-01-30T00:00:00.000Z",
"PeriodEnd": "2019-01-30T00:00:00.000Z",
"WorkedYears": 6,
"DaysPerYear": 18,
"TakenDays": 18,
"RemainingDays": 0
}
]
]
So, the final solution is get inside of the object and bring the first index on the request.object:
request.query(query, function (err, result) {
if (err) {
console.log(err);
res.send(err);
}
else {
res.send(result.recordsets[0]);
sql.close();
}
});

How do I get the sum of all the properties?

Given:
{
"property1": 10,
"property2": 20,
"property3": 30
}
I'd like to add a total property of all the properties, like this:
{
"property1": 10,
"property2": 20,
"property3": 30,
"Total": 60
}
You can use array#reduce and Object.values().
let o = { "property1": 10, "property2": 20, "property3": 30 };
o.Total = Object.values(o).reduce((s,v) => s + +v, 0);
console.log(o);

MongoDB MapReduce weird bug

I'm trying this simple MapReduce operation:
function map() {
var gameDay = Math.floor((this.matchCreation - 1427846400000) / 86400000) + 1; // day of april 2015 when the game was played
this.teams.forEach (function (team){
**team.bans.forEach(function (ban){** // says bans is undefined
var value ={
banned : 1,
firstBanned: ( ((ban.pickTurn == 1) || (ban.pickTurn == 2))? 1 : 0 )
}
emit({championId: ban.championId,
day: Number(gameDay)}, value);
emit({championId: ban.championId,
day: "all"}, value);
});
});
}
function reduce(key, values) {
var a = values[0];
for(var i = 1 ; i<values.length ; i++){
var b = values[i]; // will merge 'b' into 'a'
a.banned += (b.banned? b.banned : 0);
a.firstBanned += (b.firstBanned? b.firstBanned : 0);
for (var attrname in b){
if(attrname != "banned" && attrname != "firstBanned")
a[attrname] = b[attrname];
}
}
return a;
}
matchesCollection.mapReduce(map, reduce, {
out: { reduce: "mapReduceResults" }
}, function (err, data){
if(err)
return callback (err);
callback (null, "OK");
});
It used to work before, but just when I tried to deploy the app after testing for a while, it seems to fail in this line: team.bans.forEach(function (ban){, says team.bans is undefined, although every one of the documents has a "teams" array and a "bans" array inside of each object in it, I even double checked it by querying the database and there is no document in which those fields dont exist.
So weird. The reduce function is a bit more complex but it seems to work alright, yet the map one (unlike Reduce, its supposed to be called just once per original document, right?) throws this unexplainable error. Could anyone give me some insight?
Sample input:
{
"_id": {
"$oid": "5531a63f2a3f135c11ed14a8"
},
"matchId": 1778704162,
"region": "NA",
"platformId": "NA1",
"matchMode": "CLASSIC",
"matchType": "MATCHED_GAME",
"matchCreation": 1427864425511,
"matchDuration": 1431,
"queueType": "URF_5x5",
"mapId": 11,
"season": "SEASON2015",
"matchVersion": "5.6.0.194",
"participants": [
{
"teamId": 100,
"spell1Id": 12,
"spell2Id": 4,
"championId": 81,
"highestAchievedSeasonTier": "SILVER",
"timeline": [],
"masteries": [],
"stats": {
"winner": false,
"champLevel": 19,
"item0": 1037,
"item1": 3078,
"item2": 3117,
"item3": 3035,
"item4": 3072,
"item5": 1038,
"item6": 3340,
"kills": 7,
"doubleKills": 1,
"tripleKills": 0,
"quadraKills": 0,
"pentaKills": 0,
"unrealKills": 0,
"largestKillingSpree": 3,
"deaths": 15,
"assists": 9,
"totalDamageDealt": 103191,
"totalDamageDealtToChampions": 22148,
"totalDamageTaken": 32924,
"largestCriticalStrike": 669,
"totalHeal": 2263,
"minionsKilled": 97,
"neutralMinionsKilled": 1,
"neutralMinionsKilledTeamJungle": 1,
"neutralMinionsKilledEnemyJungle": 0,
"goldEarned": 13923,
"goldSpent": 13273,
"combatPlayerScore": 0,
"objectivePlayerScore": 0,
"totalPlayerScore": 0,
"totalScoreRank": 0,
"magicDamageDealtToChampions": 6082,
"physicalDamageDealtToChampions": 15803,
"trueDamageDealtToChampions": 263,
"visionWardsBoughtInGame": 0,
"sightWardsBoughtInGame": 0,
"magicDamageDealt": 45997,
"physicalDamageDealt": 56651,
"trueDamageDealt": 543,
"magicDamageTaken": 25249,
"physicalDamageTaken": 7490,
"trueDamageTaken": 184,
"firstBloodKill": false,
"firstBloodAssist": false,
"firstTowerKill": false,
"firstTowerAssist": false,
"firstInhibitorKill": false,
"firstInhibitorAssist": false,
"inhibitorKills": 0,
"towerKills": 4,
"wardsPlaced": 2,
"wardsKilled": 0,
"largestMultiKill": 2,
"killingSprees": 1,
"totalUnitsHealed": 1,
"totalTimeCrowdControlDealt": 98
},
"participantId": 1,
"runes": []
},
... (9 more like that)
],
"participantIdentities": [],
"teams": [
{
"teamId": 100,
"winner": false,
"firstBlood": true,
"firstTower": false,
"firstInhibitor": true,
"firstBaron": false,
"firstDragon": true,
"towerKills": 6,
"inhibitorKills": 2,
"baronKills": 0,
"dragonKills": 3,
"vilemawKills": 0,
"dominionVictoryScore": 0,
"bans": [
{
"championId": 120,
"pickTurn": 1
},
{
"championId": 37,
"pickTurn": 3
},
{
"championId": 13,
"pickTurn": 5
}
]
},
{
"teamId": 200,
"winner": true,
"firstBlood": false,
"firstTower": true,
"firstInhibitor": false,
"firstBaron": false,
"firstDragon": false,
"towerKills": 11,
"inhibitorKills": 4,
"baronKills": 0,
"dragonKills": 0,
"vilemawKills": 0,
"dominionVictoryScore": 0,
"bans": [
{
"championId": 28,
"pickTurn": 2
},
{
"championId": 38,
"pickTurn": 4
},
{
"championId": 63,
"pickTurn": 6
}
]
}
]
}
Expected output:
{
_id: { championId: Number, day: Number }
value: { banned: Number, firstBanned: Number }
}
After that, its supposed to merge with the results of a previous MapReduce operation, copying all the fields of documents with the same key (in the reduce function), but thats irrelevant now since the error happens before...

mongodb explain always returns millis as 0

I am using the node.js mongodb native driver and I have a collection with a bunch of docs inside like:
{
name: "cat",
}
I have a query which I am trying to test the speed of:
collection.find({name: { $gt: 'ca', $lt: 'cb' } }).explain(function(err, docs){
console.log( docs );
});
But the milliseconds is always 0
{ cursor: 'BasicCursor',
isMultiKey: false,
n: 2,
nscannedObjects: 8,
nscanned: 8,
nscannedObjectsAllPlans: 8,
nscannedAllPlans: 8,
scanAndOrder: false,
indexOnly: false,
nYields: 0,
nChunkSkips: 0,
millis: 0,
allPlans:
[ { cursor: 'BasicCursor',
isMultiKey: false,
n: 2,
nscannedObjects: 8,
nscanned: 8,
scanAndOrder: false,
indexOnly: false,
nChunkSkips: 0 } ],
server: 'h003723.mongolab.com:33453',
filterSet: false,
stats:
{ type: 'COLLSCAN',
works: 10,
yields: 0,
unyields: 0,
invalidates: 0,
advanced: 2,
needTime: 7,
needFetch: 0,
isEOF: 1,
docsTested: 8,
children: [] } }
I cant seem to figure this out, and obviously I cant test the query like this, am I missing something?

Resources