英文:
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: 'Gokhan', lastName: 'Coskun', login: 'gcoskun', id: 101 },
{ firstName: 'George', lastName: 'Clooney', login: 'gclooney' },
{ firstName: 'Gokhan', lastName: 'Coskun', login: 'gcoskun', id: 101 },
// Depending on the logic here, these types need to match.
// Using == instead of === so that it's not required here.
].filter(user => user.id == id)
},
};
const root = {
user: (id) => {
console.log(`ID = ${ id }`);
// We make sure we only return a single user or null if there isn't one:
return storage.select('users', id)[0] || null;
},
};
const user = root.user('101');
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: 'Gokhan', lastName: 'Coskun', login: 'gcoskun', id: 101 },
{ firstName: 'George', lastName: 'Clooney', login: 'gclooney' },
{ firstName: 'Gokhan', lastName: 'Coskun', login: 'gcoskun', id: 101 },
// Depending on the logic here, these types need to match.
// Using == instead of === so that it's not required here.
].filter(user => user.id == id);
},
};
const root = {
user: (query) => {
console.log(`ID = ${ query.id }`);
// We make sure we only return a single user or null if there isn't one:
return storage.select('users', query.id)[0] || null;
},
};
const user = root.user({ id: '101' });
console.log(user);
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论