我有一个 Wix 网站,我正在尝试向移动应用程序发出 API 请求,我有一个名为的数据库Stores
,其中有一个名为的集合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"}}
我正在尝试向移动应用程序发出 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);
}
});
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句