所以我使用@ pnp / sp来确保列表存在:
public getLocations(): Promise<ILocation[]> {
console.log("LocationServices | getLocations | fired");
return sp.web.lists.ensure(DELIVERY_BOARD_NAME)
.then((ler: ListEnsureResult): Promise<any> => {
console.log("LocationServices | getLocations | ensure complete");
if (ler.created) {
let batchCreate = sp.web.createBatch();
ler.list.fields.inBatch(batchCreate).addText('Abbreviation');
return batchCreate.execute()
.then((): Promise<any> => {
let batchDisplay = sp.web.createBatch();
let view = ler.list.defaultView;
view.fields.inBatch(batchDisplay).add('Abbreviation');
return batchDisplay.execute();
})
.then(() => ler.list.items.select("Title", "Abbreviation", "Id").get());
} else {
return ler.list.items.select("Title", "Abbreviation", "Id").get();
}
});
}
当我在沙盒的工作台中大吃大喝并玩耍时,效果很好。当我gulp bundle --ship
和gulp package-solution --ship
并将其部署到我们的租户时,它在要求列表时失败了:
LocationServices | getLocations | fired
/sites/deployedSite/SitePages/_api/web/lists:1 Failed to load resource: the server responded with a status of 404 ()
my-web-part_296a730be8d03ebb8e5ca894f4c1a0a1.js:118 Uncaught (in promise) Error: Error making HttpClient request in queryable: [404] ::> SyntaxError: Unexpected end of JSON input
at my-web-part_296a730be8d03ebb8e5ca894f4c1a0a1.js:118
单击my-web-part_296a730be8d03ebb8e5ca894f4c1a0a1.js:118
会得到一个很长的字符串,我无法理解(很确定它已被缩小)
在“网络”标签中,我有2 404个:
我假设第一个正在检查列表是否存在,如果不存在,它将发送回404(未找到)。
第二个404是pnp尝试创建列表时:
答案 0 :(得分:0)
我没有设置sp
:
public onInit(): Promise<void> {
return super.onInit().then(_ => {
sp.setup({
spfxContext: this.context
});
});
}
已于
回答全局var存在于工作台上,但不存在于已加载的Web部件的闭合中。