尝试使用 sp/pnp 将多个项目添加到 SP 列表,但在 ```createBatch()``` 上出现错误

Attempting to add multiple items to SP list using sp/pnp but getting error on ```createBatch()```

我正在关注此文档: https://pnp.github.io/pnpjs/sp/items/#add-multiple-items 但是我收到一个错误:

import { SPFI, spfi, SPFx } from "@pnp/sp";
import "@pnp/sp/webs";
import "@pnp/sp/lists";
import "@pnp/sp/items";
import "@pnp/sp/comments"
import "@pnp/sp/site-users/web";

let sp: SPFI;

export const CreateTableItems = async (listName: string, items: IMoscow) => {
    const batch = sp.web.createBatch()
};

说的是Property 'createBatch' does not exist on type 'IWeb & IInvokable<any>'.

我显然遗漏了一些东西,但文档没有说清楚。我使用的是 sp/pnp 的最新 v3 版本,我能够 submit/update 单个项目。

也许您丢失了这个导入。 导入“@pnp/sp/batching”;

https://docs.microsoft.com/es-es/sharepoint/dev/spfx/web-parts/guidance/use-sp-pnp-js-with-spfx-web-parts

似乎 createBatch 不是 sp.web 的一部分,我们需要使用 createBatch 函数并提供列表对象作为参数。在我的场景中,我想使用批处理删除多个项目并实现如下

import { SPFI, spfi, SPFx } from "@pnp/sp";
import "@pnp/sp/webs";
import "@pnp/sp/lists";
import "@pnp/sp/items";
import "@pnp/sp/batching";
import "@pnp/sp/items/get-all";
import { createBatch } from "@pnp/sp/batching";
let sp: SPFI;

export const DeleteItems = async (listName: string) => {
    const list = await sp.web.lists.getByTitle(listName);
    const items = await list.items.getAll();
    const [batchedListBehavior, execute] = createBatch(list);
    list.using(batchedListBehavior);
    items.forEach((i: IItem) => {
        list.items.getById(i["ID"]).delete();
    });
    await execute();
};

参考- Advanced Batching