Skip to content

Commit

Permalink
* fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Apollon77 committed May 27, 2022
1 parent f150a1c commit 69bf73b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
2 changes: 2 additions & 0 deletions docs/en/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ sendTo('history.0', 'storeState', [

Additionally, you can add attribute `rules: true` in message to activate all rules, like `counter`, `changesOnly`, `de-bounce` and so on.

In case of errors an array with all single error messages is returned and also a successCount to see how many entries were stored successfully.

## delete state
If you want to delete entry from the Database you can use the build in system function **delete**:

Expand Down
10 changes: 5 additions & 5 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -1828,13 +1828,12 @@ async function storeState(msg) {
}, msg.callback);
}

let id;
let errors = [];
let successCount = 0;
if (Array.isArray(msg.message)) {
adapter.log.debug(`storeState: store ${msg.message.length} states for multiple ids`);
for (let i = 0; i < msg.message.length; i++) {
id = aliasMap[msg.message[i].id] ? aliasMap[msg.message[i].id] : msg.message[i].id;
const id = aliasMap[msg.message[i].id] ? aliasMap[msg.message[i].id] : msg.message[i].id;
try {
storeStatePushData(id, msg.message[i].state, msg.message.rules);
successCount++;
Expand All @@ -1844,7 +1843,7 @@ async function storeState(msg) {
}
} else if (msg.message.state && Array.isArray(msg.message.state)) {
adapter.log.debug(`storeState: store ${msg.message.state.length} states for ${msg.message.id}`);
id = aliasMap[msg.message.id] ? aliasMap[msg.message.id] : msg.message.id;
const id = aliasMap[msg.message.id] ? aliasMap[msg.message.id] : msg.message.id;
for (let j = 0; j < msg.message.state.length; j++) {
try {
storeStatePushData(id, msg.message.state[j], msg.message.rules);
Expand All @@ -1855,15 +1854,15 @@ async function storeState(msg) {
}
} else {
adapter.log.debug(`storeState: store 1 state for ${msg.message.id}`);
id = aliasMap[msg.message.id] ? aliasMap[msg.message.id] : msg.message.id;
const id = aliasMap[msg.message.id] ? aliasMap[msg.message.id] : msg.message.id;
try {
storeStatePushData(id, msg.message.state, msg.message.rules);
successCount++;
} catch (err) {
errors.push(err.message);
}
}
if (error.length) {
if (errors.length) {
adapter.log.warn(`storeState executed with ${errors.length} errors: ${errors.join(', ')}`);
return adapter.sendTo(msg.from, msg.command, {
error: `${errors.length} errors happened while storing data`,
Expand All @@ -1872,6 +1871,7 @@ async function storeState(msg) {
}, msg.callback);
}

adapter.log.debug(`storeState executed with ${successCount} states successfully`);
adapter.sendTo(msg.from, msg.command, {success: true, successCount}, msg.callback);
}

Expand Down

0 comments on commit 69bf73b

Please sign in to comment.