0

我正在一个 WIX 网站上工作,该网站发布了人们可以注册的这些“事件”。我试图确保这些事件在事件结束时从前端隐藏。我创建了一个“显示”布尔值,中继器检查显示值是否为真。如果事件结束,我想要做的是使“显示”值变为假。

每次有人访问查询数据库中任何早于今天日期的项目并将这些项目“显示”值设置为 false 的页面时,我都会调用一个方法来实现这一点。问题是页面在发生这种情况时需要一段时间才能加载。虽然我想做的是使用 cron 服务每天调用几次方法。我能看到的唯一方法是使用 wix-http 函数来调用该方法。到目前为止,这是我的代码:

import wixData from 'wix-data';
import {ok} from 'wix-http-functions';

export function use_eventExpire() {
    var today = new Date();     //Gets todays date
    return wixData.query("Events")  //Begins Query of "Events" Database
        .le("endTime", today)   //Looks for items that are older than 
//today's date
        .eq("show", true)       //Looks for items that have the "show" 
//value as true
        .find()                 //Ends query
        .then((results) => {    //Gets Results
            let numberOfItems = results.length;     //Gets Number 
//of Items from Query
            var i = 0;                              //Declares iterator
            for (; i < numberOfItems; i++) {  //Loops through all items
                let items = results.items[i]; //Loops through all items
                items.show = false; //Sets items "show" value to false
                wixData.update("Events", items)     //Updates Database
                    .then((results) => {            //If successful
                        console.log("Successfull: " + results);     //Logs Results
                    }).catch((err) => {             //If unsuccessful
                        console.log("There was an Error: " + err);  
//Logs Error
                    });
        }
    });
}

当我转到链接时,它返回 ok,但没有调用任何内容并且数据库没有更新。我需要改变什么?

4

1 回答 1

0

您使用 Promise 和 return 语句存在问题。您应该包括 bluebird(例如)以帮助您遍历数组。 http://bluebirdjs.com/docs/getting-started.html

// make sure you include bluebird - not sure if you are working client side or server side

function updateEvent(item) {
    item.show = false;
    return wixData.update("Events", items);
}

export function use_eventExpire() {
    var today = new Date(); //Gets todays date
    Promise.resolve().then(() => {
        return wixData.query("Events") //Begins Query of "Events" Database
            .le("endTime", today) //Looks for items that are older than today's date
            .eq("show", true) //Looks for items that have the "show" value as true
            .find(); //Ends query
    }).then((results) => { //Gets Results
        return Promise.map(results, updateEvent);
    }).catch((err) => {
        console.log(err);
    }).finally(() => {
        console.log("all finished");
    });
}
于 2018-02-27T17:42:39.200 回答