Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

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

How to use mget with a variable of type array #48

Open
awerhane opened this issue Jun 21, 2020 · 3 comments
Open

How to use mget with a variable of type array #48

awerhane opened this issue Jun 21, 2020 · 3 comments
Labels
question Further information is requested

Comments

@awerhane
Copy link

I'm assuming this is just a syntax issue, but I can't seem to use mget with a variable.

If I have a variable
const keys = ["record1", "record2", "record3"]

The following will return 0 records
const records = await tedis.mget(keys)

But if I do the following, I will get 3 records.
const records = await tedis.mget("record1", "record2", "record3")

I'm not sure of the syntax when using a variable.
I even tried the following, but I still get no results.
const keyString = '"record1", "record2", "record3"'
const records = await tedis.mget(keyString)

Any help on how to execute an mget request with a variable (preferably an array) would be appreciated.

Thanks!

@dasoncheng
Copy link
Contributor

dasoncheng commented Nov 3, 2020

The current version does not support the kind of call method you said, you can refer to the documentation about the use of mget. tedis later upgrades will add this overload method, you can use the new feature of es' Spread syntax to implement the requirements.

const keys = ["record1", "record2", "record3"]
const records = await tedis.mget(...keys)

@dasoncheng dasoncheng added the question Further information is requested label Nov 3, 2020
@AlphaBs
Copy link

AlphaBs commented Oct 13, 2021

The current version does not support the kind of call method you said, you can refer to the documentation about the use of mget. tedis later upgrades will add this overload method, you can use the new feature of es' Spread syntax to implement the requirements.

const keys = ["record1", "record2", "record3"]
const records = await tedis.mget(...keys)

This codes does not work in TypeScript:

    const clientHashes: string[] = clientFiles.map(f => f.hash);
    let serverPaths: string[];

    // Error: ts(2556) A spread argument must either have a tuple type or be passed to a rest parameter
    serverPaths = await db.hmget(filesKey, ...clientHashes);

    // I should write code like this: 
    if (clientHashes.length == 1)
        serverPaths = await db.hmget(filesKey, clientHashes[0]);
    else if (clientHashes.length > 1)
        serverPaths = await db.hmget(filesKey, clientHashes[0], ...clientHashes.slice(1));

Why hmget function has field parameter?

  public hmget(key: string, field: string, ...fields: string[]) {
    return this.command<Array<string | null>>("HMGET", key, field, ...fields);
  }

=>

  public hmget(key: string, ...fields: string[]) {
    return this.command<Array<string | null>>("HMGET", key, ...fields);
  }

I think above two codes is same. Is there any compatibility problem?

@dasoncheng
Copy link
Contributor

@AlphaBs Thank you for your feedback, the version switch will be completed this month and the interface will be resolved and optimized by then

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants