With PNP JS, How do I handle odata.null return values?
-
21-01-2021 - |
Pregunta
I'm using @pnp in my SPFX project, and I need to get tenant property. The code is as followed:
sp.web.getStorageEntity('mytenantproperty1')
.then((prop: StorageEntity) => {
//How do I handle odata.null?
return prop.value;
});
If the key doesn't exist the results I get back is
Object {odata.metadata: "https://_________.sharepoint.com/sites/ASite/_a…", odata.null: true} odata.metadata:"https://________.sharepoint.com/sites/ASite/_api/$metadata#Edm.Null" odata.null:true proto:Object {constructor: , defineGetter: , defineSetter: , …}
I've tried to check odata.null value but it doesn't appear to be there. So how do I check for invalid key?
Solución
You can check odata.null
in the prop object itself.
Using this snippet you can check for invalid key:
sp.web.getStorageEntity('mytenantproperty1')
.then((prop: StorageEntity) => {
console.log(prop);
if(prop["odata.null"]){
// storage entity doesnt exist
// do something (like create entity or return empty string)
return "";
}
// storage entity exists, return its value
return prop.Value;
});
Licenciado bajo: CC-BY-SA con atribución
No afiliado a sharepoint.stackexchange