【问题标题】:How to get access to Wix DB - Blog Posts如何访问 Wix DB - 博客文章
【发布时间】:2020-09-11 13:57:23
【问题描述】:

所以现在我有一个使用 html 制作的主页。 我想添加一些 div,在其中显示我在 WIX 页面上发布的最新博客。

<div layout="row" layout-align="center center">
   <md-card flex="60" class="pad-md md-body-1 border-1" md-colors="{&quot;borderColor&quot;: &quot;epprimary1-500&quot;, &quot;color&quot;: &quot;epsecondary6&quot;}">
      {{blog headline}}
      <a href="{{blog link}}">Open Blog<md-icon>open_in_new</md-icon></a>
   </md-card>
</div>

在 Wix 平台上,我知道他们将数据存储在所谓的数据集中的什么位置:

现在我需要知道如何从我的其他网站访问这些数据。

【问题讨论】:

    标签: javascript node.js angularjs velo


    【解决方案1】:

    我终于明白了!!

    您可以通过 http 请求获取所需的数据。 因此,首先,您需要在 Wix 的后端文件夹中添加一个 javascript 并将其命名为“http-functions.js”,删除其内容并添加以下代码。

    注意:get_blogEntry() 是method_functionName()

    Blog/Posts 是我使用的数据库,您可以使用 wix 上的任何数据库。

    import {ok, notFound, serverError} from 'wix-http-functions';
    import wixData from 'wix-data';
    export function get_blogEntry() {
      let options = {
        "headers": {
        "Content-Type": "application/json",
        "Access-Control-Allow-Origin": "*"
        }
      };
    // query a collection to find matching items
    return wixData.query("Blog/Posts")
    .find()
    .then( (results) => {
      // matching items were found
      if(results.items.length > 0) {
        let itemOne = results.items[0];
        let itemTwo = results.items[1];
    
        options.body = {
            "blogOneTitle": itemOne.title,
            "blogOneUrl": "https://etaplus.energy" + itemOne.postPageUrl,
            "blogTwoTitle": itemTwo.title,
            "blogTwoUrl": "https://etaplus.energy" + itemTwo.postPageUrl
        }
        return ok(options);
      }
    })
    // something went wrong
    .catch( (error) => {
      options.body = {
        "error": error
      };
      return serverError(options);
    } );
    }
    

    在后端添加此代码后,您可以通过以下 URL 访问数据:

    “https://YOURWEBSITEURL/_functions/blogEntry或任何你的函数名称

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-05
      • 2016-05-18
      • 2016-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多