You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
By clicking “Sign up for GitHub”, you agree to our
terms of service
and
privacy statement
. We’ll occasionally send you account related emails.
Already on GitHub?
Sign in
to your account
del function always return false
I want to get
true
if delete is successfully completed and
false
else. But
del
function always return false.
Environment
Node.js Version
:
14.7.0
Redis Version
:
5.0.4
Platform
:
Manjaro x64 20.0.3
You need to use
callbacks
in order to check the result of
del
command.
const redis = require("redis");
const client = redis.createClient();
client.set("test", "value", (err) => {
if (err) {
console.error(err);
} else {
client.del("test", (err2, numberOfDeletedKeys) => {
if (err2) {
console.error(err2);
} else {
console.log(numberOfDeletedKeys); // <-- this prints 1 if no error happened
client.quit();
});
});
To avoid using
callbacks
consider using
promises
.
const redis = require("redis");
const client = redis.createClient();
const util = require("util");
const set = util.promisify(client.set).bind(client);
const del = util.promisify(client.del).bind(client);
set("test2", "value")
.then(() => del("test2"))
.then((numberOfDeletedKeys) => {
console.log(numberOfDeletedKeys)
.catch((err) => {
console.error(err);
.finally(() => {
client.quit();
});