Thank you for reporting an issue, suggesting an enhancement, or asking a question. We appreciate your feedback - to help the team understand your
needs please complete the below template to ensure we have the details to help. Thanks!
Please check out the Docs to see if your question is already addressed there. This will help us ensure our documentation covers the most frequent questions.
Please specify what version of the library you are using: [ 2.0.6 ]
Please specify what version(s) of SharePoint you are targeting: [ sharepoint online ]
If you are not using the latest release, please update and see if the issue is resolved before submitting an issue.
If you are reporting an issue please describe the expected behavior. If you are suggesting an enhancement please
describe thoroughly the enhancement, how it can be achieved, and expected benefit. If you are asking a question, ask away!
I tried to upload attachment of list item with this sample code page- https://pnp.github.io/pnpjs/sp/attachments/,
but I got below error.
TypeError: Cannot read property 'add' of undefined
Here is my code:
import { sp } from "@pnp/sp";
import { IItem } from "@pnp/sp/items";
import "@pnp/sp/webs";
import "@pnp/sp/lists/web";
import "@pnp/sp/items";
import "@pnp/sp/attachments";
const uploadattachment = async (iid: number) => {
let _spitem: IItem = await sp.web.lists.getByTitle("Complaint List").items.getById(iid)();
console.log(typeof (_spitem)); //--> console display my list item information, it's correct
console.log(typeof (_spitem.attachmentFiles)); //--> console display "undefined"
await _spitem.attachmentFiles.add("file2.txt", "Here is my content");
};
Did I missing something? I would appreciate you can help to look at. Thanks.
Thank you for your feedback!
This issue is somewhat similar to #1194
Try:
await sp.web.lists.getByTitle("MyList").items.getById(1).attachmentFiles.add(...)
The underlying issue is under investigation.
let _spitem: IItem = await list.items.getById(iid)(); // <-- this gets item data, not `IItem` API object
console.log(typeof (_spitem.attachmentFiles)); //--> console display "undefined" // <-- this is expected and correct
should be:
const item = list.items.getById(iid); // <-- no await and self-invoking when IItem needed
item.attachmentFiles. // ...;
This issue is somewhat similar to #1194
Try:
await sp.web.lists.getByTitle("MyList").items.getById(1).attachmentFiles.add(...)The underlying issue is under investigation.
Thanks. @juliemturner
It works now via your code.
I think this issue can be closed and I will follow #1194 result.
You're probably right, I think I'll leave it open as a reminder that we need to check all the attachmentFiles methods, but I suspect once we fix attachmentFiles on the item that it'll all be resolved.
Going to close this as I think it was indeed a docs issue and is now resolved. Thanks all!
Most helpful comment
This issue is somewhat similar to #1194
Try:
await sp.web.lists.getByTitle("MyList").items.getById(1).attachmentFiles.add(...)The underlying issue is under investigation.