`cf.asOrganization` is `undefined` for some requests

Ran into this in production today where req.cf.asOrganization was undefined for some requests. The IncomingRequestCfProperties docs show this as being a string and not optional. Was able to fix the issue by changing our code (which is based on worker-sys) to treat the property as optional:
#[wasm_bindgen(method, catch, getter, js_name = asOrganization)]
pub fn as_organization(this: &IncomingRequestCfProperties) -> Result<Option<String>, JsValue>;
#[wasm_bindgen(method, catch, getter, js_name = asOrganization)]
pub fn as_organization(this: &IncomingRequestCfProperties) -> Result<Option<String>, JsValue>;
Was going to contribute this back to worker-sys, but when I consulted the docs and saw that it is denoted as non-optional I wanted to see whether this is expected to always be present. And if cf.asOrganization is always intended to be there, why is it missing from some requests?
4 Replies
Walshy
Walshy5mo ago
It can be optional, do not rely on it existing
maxdeviant
maxdeviantOP5mo ago
Okay, so should the docs be updated to reflect that?
Walshy
Walshy5mo ago
yes they should be, a lot of the geo-ip and ip-related info can be optional as we may not always have it
maxdeviant
maxdeviantOP5mo ago
I went ahead and opened a PR to upstream the fix to the worker crate: https://github.com/cloudflare/workers-rs/pull/776
GitHub
Make as_organization optional on IncomingRequestCfProperties by...
This PR updates the IncomingRequestCfProperties to reflect that as_organization is optional. We ran into a case today in production where cf.asOrganization was undefined. We&amp;#39;re not using wo...

Did you find this page helpful?