如何正确传递参数?

huangapple go评论70阅读模式
英文:

How can I pass parameter correctly?

问题

I want to call the arrow function in root.user, but I think I can't pass the parameter correctly, so I tried this: let user = root.user('101'), and on the console, I got this:

returning object undefined

[{"firstName":"Gokhan","lastName":"Coskun","login":"gcoskun","id":101}]
{"firstName":"George","lastName":"Clooney","login":"gclooney"}
[{"firstName":"Gokhan","lastName":"Coskun","login":"gcoskun","id":101}]

I wanted the user with the id 101 to get returned, but instead, I got all of the users returned.

英文:
const root = {
    user: (id) => {
        console.log("returning object " + JSON.stringify(id.id) + " " + JSON.stringify(storage.select("users", id.id)))
        return storage.select("users", id.id)
    }
}

I want to call the arrow function in root.user but I think I can't pass the parameter correctly, so I tried this --> let user = root.user('101')
and on the console I got this -->

returning object undefined

[{"firstName":"Gokhan","lastName":"Coskun","login":"gcoskun","id":101}]
{"firstName":"George","lastName":"Clooney","login":"gclooney"}
[{"firstName":"Gokhan","lastName":"Coskun","login":"gcoskun","id":101}]

I wanted the user with the id 101 get returned and got instead all of the users returned.

答案1

得分: 2

以下是翻译的内容:

为什么你在做 id.id,但传递的是一个 string?你可以要么传递一个带有 id 属性的对象(root.user({ id: '101' })),要么将 id.id 替换为简单的 id

另外,看起来你的用户对象中的 id 字段是 number 类型,但你传递的是一个 string,所以根据 storage.select 内部的逻辑,你可能需要进行更改。

传递一个 number 类型的 id

// 仅供示例使用的模拟对象:
const storage = {
  select(key, id) {
    return [
      { firstName: 'Gokhan', lastName: 'Coskun', login: 'gcoskun', id: 101 },
      { firstName: 'George', lastName: 'Clooney', login: 'gclooney' }, 
      { firstName: 'Gokhan', lastName: 'Coskun', login: 'gcoskun', id: 101 },
      // 根据此处的逻辑,这些类型需要匹配。
      // 使用 == 而不是 === 以便在这里不是必需的。
    ].filter(user => user.id == id)
  },
};

const root = {
  user: (id) => {
      console.log(`ID = ${ id }`);
      
      // 确保我们只返回一个用户或者如果没有用户则返回 null:
      return storage.select('users', id)[0] || null;
  },
};

const user = root.user('101');

console.log(user);

传递一个带有 number 类型的 id 属性的对象:

// 仅供示例使用的模拟对象:
const storage = {
  select(key, id) {
    return [
      { firstName: 'Gokhan', lastName: 'Coskun', login: 'gcoskun', id: 101 },
      { firstName: 'George', lastName: 'Clooney', login: 'gclooney' }, 
      { firstName: 'Gokhan', lastName: 'Coskun', login: 'gcoskun', id: 101 },
      // 根据此处的逻辑,这些类型需要匹配。
      // 使用 == 而不是 === 以便在这里不是必需的。
    ].filter(user => user.id == id);
  },
};

const root = {
  user: (query) => {
      console.log(`ID = ${ query.id }`);
      
      // 确保我们只返回一个用户或者如果没有用户则返回 null:
      return storage.select('users', query.id)[0] || null;
  },
};

const user = root.user({ id: '101' });

console.log(user);
英文:

Why are you doing id.id but passing a string? You either pass an object with an id prop (root.user({ id: '101' })) or replace id.id with simply id.

Also, it looks like the id fields in your user objects are of type number, while you are passing a string, so depending on the logic inside storage.select you might have to change that.

Passing a number id:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

// Just mocking it for the example:
const storage = {
  select(key, id) {
    return [
      { firstName: &#39;Gokhan&#39;, lastName: &#39;Coskun&#39;, login: &#39;gcoskun&#39;, id: 101 },
      { firstName: &#39;George&#39;, lastName: &#39;Clooney&#39;, login: &#39;gclooney&#39; }, 
      { firstName: &#39;Gokhan&#39;, lastName: &#39;Coskun&#39;, login: &#39;gcoskun&#39;, id: 101 },
      // Depending on the logic here, these types need to match.
      // Using == instead of === so that it&#39;s not required here.
    ].filter(user =&gt; user.id == id)
  },
};

const root = {
  user: (id) =&gt; {
      console.log(`ID = ${ id }`);
      
      // We make sure we only return a single user or null if there isn&#39;t one:
      return storage.select(&#39;users&#39;, id)[0] || null;
  },
};


const user = root.user(&#39;101&#39;);

console.log(user);

<!-- end snippet -->

Passing an object with an id prop of type number:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

// Just mocking it for the example:
const storage = {
  select(key, id) {
    return [
      { firstName: &#39;Gokhan&#39;, lastName: &#39;Coskun&#39;, login: &#39;gcoskun&#39;, id: 101 },
      { firstName: &#39;George&#39;, lastName: &#39;Clooney&#39;, login: &#39;gclooney&#39; }, 
      { firstName: &#39;Gokhan&#39;, lastName: &#39;Coskun&#39;, login: &#39;gcoskun&#39;, id: 101 },
      // Depending on the logic here, these types need to match.
      // Using == instead of === so that it&#39;s not required here.
    ].filter(user =&gt; user.id == id);
  },
};

const root = {
  user: (query) =&gt; {
      console.log(`ID = ${ query.id }`);
      
      // We make sure we only return a single user or null if there isn&#39;t one:
      return storage.select(&#39;users&#39;, query.id)[0] || null;
  },
};

const user = root.user({ id: &#39;101&#39; });

console.log(user);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2020年1月6日 23:24:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/59614681.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定