首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试使用Wix Velo和Promises返回ID

尝试使用Wix Velo和Promises返回ID
EN

Stack Overflow用户
提问于 2021-09-10 17:57:11
回答 2查看 175关注 0票数 0

我正在尝试使用我的登录用户的Wix Velo检索_id字段。代码是有效的,但是我使用的函数返回的是object Promise,而不是我需要的id字符串。

代码语言:javascript
复制
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,但它只会给我错误。

EN

回答 2

Stack Overflow用户

发布于 2021-09-10 23:04:49

由于顶层的所有浏览器都在等待is not supported,我相信Wix也没有开放该功能。取而代之的是,用异步IIFE包装它。

代码语言:javascript
复制
(async () => {
  console.log("Return the ID outside of the function: " + await getID())
})();

answer包含有关顶级等待的更多详细信息

票数 0
EN

Stack Overflow用户

发布于 2021-09-13 05:08:14

为了防止这对其他人有帮助,答案就在嵌套的承诺中。我必须调用第一个函数getLoggedInUserName()来获取用户名,然后将其传递给一个名为getID()的嵌套函数。

本质上,这是一个范围问题,也在等待承诺的解决。

代码语言:javascript
复制
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;
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69136243

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档