[英]Javascript hasOwnProperty(key) === false but property exists
I have an object called context
and these logs:我有一个名为context
的 object 和这些日志:
console.log({
context,
"Object.keys(context)": Object.keys(context),
'context.hasOwnProperty("status")': context.hasOwnProperty("status"),
"context.status": context.status,
});
Here's the output:这是 output:
context
cache: InMemoryCache
client: ApolloClient
clientAwareness {...}
forceFetch: false
getCacheKey: f()
status: 3
Object.keys(context): Array(5)
0: "forceFetch"
1: "cache"
2: "getCacheKey"
3: "clientAwareness"
4: "client"
context.hasOwnProperty("status"): false
context.status: undefined
How can it possibly be that the status
property is defined and has a value of 3, but "Object.keys", "hasOwnProperty" and accessing it indicate that it doesn't exist?怎么可能定义了status
属性并且其值为 3,但是“Object.keys”、“hasOwnProperty”和访问它表明它不存在?
Here's a CodeSandbox with my code.这是一个带有我的代码的CodeSandbox 。 If you open ./apollo/resolvers/bar
, you'll see the logs for the context
object.如果您打开./apollo/resolvers/bar
,您将看到context
object 的日志。
I found out what was going on.我发现发生了什么事。 The order of the fields in my GraphQL query was not correct, in that the resolver that was logging the context was defined before the resolver for status
.我的 GraphQL 查询中的字段顺序不正确,因为记录上下文的解析器是在status
解析器之前定义的。 See my answer below.请看下面我的回答。
It turns out the issue was caused by a weird behaviour of Apollo.事实证明,这个问题是由 Apollo 的奇怪行为引起的。
In my GraphQL query, I defined status
below the resolver where I was logging the values of context
:在我的 GraphQL 查询中,我在解析器下方定义了status
,我在其中记录了context
的值:
query Foo {
id
bar @client
status @client
}
Moving status
to the top fixes the issue:将status
移至顶部可解决问题:
query Foo {
id
status @client
bar @client
}
Make sure your property is enumerable.确保您的属性是可枚举的。 Respond with more code where you are creating this object [See here][1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Enumerability_and_ownership_of_properties在您创建此 object [参见此处][1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Enumerability_and_ownership_of_properties的位置响应更多代码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.