【问题标题】:How to make http request using wix?如何使用wix发出http请求?
【发布时间】:2019-10-24 16:12:49
【问题描述】:

我有一个 Wix 网站,我正在尝试向移动应用程序发出 API 请求 我有一个名为 Stores 的数据库,其中有一个名为 Products 的集合

我的问题是如何发出获取请求以获取集合 Products 中的所有值? 我下面的代码不工作

http-functions.js

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


export function get_example(request) {
    const response = {
        "headers": {
            "Content-Type": "application/json"
        }
    };

    return wixData.query("Products")
    .find()
    .then(
        result=> {
            response.body = {
            "items": result.items
        };
        return ok(response);
        }
    )
}

我收到此错误

{"error":{"name":"Error","errorGroup":"User","code":"WD_VALIDATION_ERROR"}}

【问题讨论】:

    标签: velo


    【解决方案1】:

    我正在尝试向移动应用发出 API 请求

    您正在尝试将 API 请求 TO 发送到移动应用程序?如果是这样,您需要使用 wix-fetch。

    如果您尝试移动应用您的 Wix 网站发出请求,那么您就在正确的轨道上。您需要拨打如下电话。

    import {ok, notFound, serverError} from 'wix-http-functions';
    import wixData from 'wix-data';
    
    export function get_getNewItems(request) {
        let options = {
        "headers": {
            "Content-Type": "application/json"
            }
        };
        return getItems(options)
        .catch( (error) => {
            options.body = {
                "origin": "server error",
                "error": error
            };
            return serverError(options);
        });
    }
    
    function getItems(options) {
        return wixData.query("Stores/Products")
        .find()
        .then( (results) => {
          // matching items were found
            if(results.items.length > 0) {
                options.body = {
                    "origin": "success",
                    "items": results.items
                };
                return ok(options);
            } else {
            // no matching items found
                options.body = {
                    "origin": "no items",
                    "error": 'No items found'
                };
                return notFound(options);
            }
            });
    }
    

    【讨论】:

      猜你喜欢
      • 2019-10-20
      • 1970-01-01
      • 2022-07-02
      • 1970-01-01
      • 2021-03-08
      • 2013-11-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-28
      相关资源
      最近更新 更多