fixed mistake in examples

This commit is contained in:
Naman Jain
2022-02-03 09:29:42 +00:00
parent aea7054938
commit adc8bf37fe
4 changed files with 37 additions and 17 deletions

View File

@@ -3,13 +3,13 @@ var app = express();
var actions = new Map();
actions.put("play", function play(data) {
// ...
// ...
});
actions.put("pause", function pause(data) {
// ...
// ...
});
app.get('/perform/:action/:payload', function(req, res) {
let action = actions.get(req.params.action);
res.end(action.get(req.params.payload)); // NOT OK
});
let action = actions.get(req.params.action);
res.end(action(req.params.payload)); // NOT OK
});

View File

@@ -3,15 +3,15 @@ var app = express();
var actions = new Map();
actions.put("play", function play(data) {
// ...
// ...
});
actions.put("pause", function pause(data) {
// ...
// ...
});
app.get('/perform/:action/:payload', function(req, res) {
if (actions.has(req.params.action)){
let action = actions.get(req.params.action);
res.end(action.get(req.params.payload)); // NOT OK, but not flagged [INCONSISTENCY]
}
});
if (actions.has(req.params.action)) {
let action = actions.get(req.params.action);
res.end(action(req.params.payload)); // NOT OK, but not flagged [INCONSISTENCY]
}
});

View File

@@ -0,0 +1,20 @@
var express = require('express');
var app = express();
var actions = new Map();
actions.put("play", function play(data) {
// ...
});
actions.put("pause", function pause(data) {
// ...
});
app.get('/perform/:action/:payload', function(req, res) {
if (typeof actions.get(req.params.action) === 'function') {
let action = actions.get(req.params.action); // OK but flagged [INCONSISTENCY]
// GOOD: `action` is either the `play` or the `pause` function from above
res.end(action(req.params.payload));
} else {
res.end("Unsupported action.");
}
});

View File

@@ -9,12 +9,12 @@ actions.put("pause", function pause(data) {
// ...
});
app.get('/perform/:action/:payload', function (req, res) {
if (typeof actions.get(req.params.action) === 'function') {
let action = actions.get(req.params.action);
// GOOD: `action` is either the `play` or the `pause` function from above
app.get('/perform/:action/:payload', function(req, res) {
let action = actions.get(req.params.action);
// GOOD: `action` is either the `play` or the `pause` function from above
if (typeof action === 'function') {
res.end(action(req.params.payload));
} else {
res.end("Unsupported action.");
}
});
});