我正在尝试使用我的登录用户的Wix Velo检索_id字段。代码是有效的,但是我使用的函数返回的是object Promise,而不是我需要的id字符串。
import wixData from 'wix-data';
export function getID() {
return wixData.query("Picks") // My Collection
.eq("player", "Gary") // A field, and a cell
.find()
.then((results) => {
if (results.items.length > 0) {
let firstItem = results.items[0]._id;
console.log("Return ID: " + firstItem) // This returns the ID I need
$w("#text31").text = firstItem;
// return firstItem.toString();
} else {
console.log("No Items Found")
}
})
.catch((err) => {
let errorMsg = err;
});
}
console.log("Return the ID outside of the function: " + getID()) // This returns "[object Promise]"我曾尝试使用await,但它只会给我错误。
发布于 2021-09-10 23:04:49
由于顶层的所有浏览器都在等待is not supported,我相信Wix也没有开放该功能。取而代之的是,用异步IIFE包装它。
(async () => {
console.log("Return the ID outside of the function: " + await getID())
})();此answer包含有关顶级等待的更多详细信息
发布于 2021-09-13 05:08:14
为了防止这对其他人有帮助,答案就在嵌套的承诺中。我必须调用第一个函数getLoggedInUserName()来获取用户名,然后将其传递给一个名为getID()的嵌套函数。
本质上,这是一个范围问题,也在等待承诺的解决。
getLoggedInUserName().then((results) => {
let userName = results;
getID(userName).then((results) => {
let userID = results;
let pick = $w("#dropdown1").value;
let toUpdate = {
"_id": userID,
"pick": pick,
"player": userName,
}
wixData.update("Picks", toUpdate)
.then((results) => {
$w("#text33").show();
let item = results;
})
.catch((err) => {
let errorMsg = err;
console.log("Error: " + errorMsg)
});
})
})
.catch((err) => {
let errorMsg = err;
});https://stackoverflow.com/questions/69136243
复制相似问题