🐛 [Bug]: 从数据源抓取数据函数有fetchData.api.apply is not a function
Closed this issue · 6 comments
Environment
win11, Command Prompt
Version
v20.10.0
Version
`-- (empty)
Link to minimal reproduction
Step to reproduce
- 自建一个数据 server, 安装
express
包并且用 node.js 运行:
const express = require('express');
const app = express();
const data = [
{ data_id: '1', data_name: 'data1' },
];
app.get('/data', (req, res) => {
res.json(data);
});
app.listen(8000, () => {
console.log('Listening on port 8000');
});
- 输入:
curl http://localhost:8000/data
- 输出:
[{"data_id":"1","data_name":"data1"}]
名 | 值 |
---|---|
数据源名称 | FakeDataSource |
描述 | Fake Data Source |
请求地址 | http://localhost:8000/data |
请求结果 | {"data_id":"1","data_name":"data1"} |
title | field |
---|---|
data_id | data_id |
data_name | data_name |
TypeError: fetchData.api.apply is not a function
at Proxy.handleFetch (tiny-vue.mjs:37526:32)
at Proxy.commitProxy (tiny-vue.mjs:37679:14)
at Proxy.mounted13 (tiny-vue.mjs:37334:12)
at callWithErrorHandling (vue.runtime.esm-browser.js:1651:22)
at callWithAsyncErrorHandling (vue.runtime.esm-browser.js:1660:21)
at hook.__weh.hook.__weh (vue.runtime.esm-browser.js:4145:29)
at flushPostFlushCbs (vue.runtime.esm-browser.js:1848:47)
at render (vue.runtime.esm-browser.js:7601:9)
at mount (vue.runtime.esm-browser.js:5845:25)
at app.mount (vue.runtime.esm-browser.js:10755:23)
about:srcdoc:168
What is expected
预期在预览页面里,可以从数据源正常抓取数据,并且显示在表格里
What is actually happening
No response
Any additional comments (optional)
No response
@yaoyun8 我按照官方教程尝试了:https://opentiny.design/tiny-engine#/help-center/course/engine/11
- 首先拖了个 Collections,然后在 Collections 里面放一个 content 组件的表格组件。系统提示侦测到现有的数据源,我选择了预填充自动填进去。接着,我在空荡荡的表格里重新创建了两个表格列 data_id 和 data_name,每个列的 title 和 field 分别为:
title | field |
---|---|
data_id | data_id |
data_name | data_name |
- 打开 JS,里面有一些自动产生的代码:
function getTableData25644f43({ page, sort, sortBy, filters }) {
/**
* @param {Object} sort 排序数据
* @param {Array} sortBy 排序方式
* @param {Object} page 分页数据
* @param {Array} filters 筛选数据
* @returns {Object} 返回一个promise对象,并且resolve格式为{ result: Array, page: { total: Number } }
*/
return new Promise((resolve, reject) => {
this.dataSourceMap.undefined.load().then((res) => {
// 如果按照数据源面板的建议格式编写dataHandler
// 那么dataSourceMap的res格式应该是:{ code: string, msg: string, data: {items: any[], total: number} }
resolve({ result: res.data.items, page: { total: res.data.total } })
})
})
}
- 保存后有一个错误信息弹出,估计是跟上面代码的
this.dataSourceMap.undefined.load()
有关:
- 所以我把
this.dataSourceMap.undefined.load()
换成this.dataSourceMap.FakeDataSource.load()
,其中FakeDataSource
是编辑器自动建议弹出来的,所以应该不存在大小写的分别:
- 再次保存后预览,没有报错,但也没有载入数据:
@yaoyun8 解决了 undefined 的问题了。发现是要先对 Collections 选取数据源,之后再拉表格,才会自动填入。
目前的问题是数据没有在表格里显示。Console 显示预览状态下,应用有访问到 API 伺服器,也没有报错。以下是我的数据源的配置,是跟着这个教程配的 https://opentiny.design/tiny-engine#/help-center/course/engine/9:
名 | 值 |
---|---|
数据源名称 | FakeDataSource |
描述 | Fake Data Source |
请求地址 | GET; http://localhost:8000/fakeData |
请求完成回调函数(dataHandler) | function dataHandler(data) { return data.map(v => { return { name: v.aa.ss, status: v.status } }) } |
请求结果 | {"data_id":"1","data_name":"data1"} |
我使用的自建 API 伺服器是这样的:
const express = require('express');
const app = express();
const data = [{ data_id: '1', data_name: 'data1' }];
app.get('/fakeData', (req, res) => {
res.json(data);
});
app.get('/fakeDataWellFormatted', (req, res) => {
res.json({
"code": "200",
"msg": "OK",
"data": {
"items": data,
"total": data.length
}
});
});
app.listen(8000, () => {
console.log('Listening on port 8000');
});
是 API 伺服器返回的结果有问题,导致表格无法读取数据吗?还是哪一步配置了呢?谢谢!
Bot detected the issue body's language is not English, translate it automatically.
The problem of undefined is solved. I found that I had to select the data source for Collections first, and then pull the table before it would be filled in automatically.
The current problem is that the data is not displayed in the table. When the console displays the preview state, the application has accessed the API server and no error was reported. The following is the configuration of my data source, which is configured following this tutorial https://opentiny.design/tiny-engine#/help-center/course/engine/9:
name | value |
---|---|
Data source name | FakeDataSource |
Description | Fake Data Source |
Request address | GET; http://localhost:8000/fakeData |
Request completion callback function (dataHandler) | function dataHandler(data) { return data.map(v => { return { name: v.aa.ss, status: v.status } }) } |
Request result | {"data_id":"1","data_name":"data1"} |
The self-built API server I use is like this:
const express = require('express');
const app = express();
const data = [{ data_id: '1', data_name: 'data1' }];
app.get('/fakeData', (req, res) => {
res.json(data);
});
app.get('/fakeDataWellFormatted', (req, res) => {
res.json({
"code": "200",
"msg": "OK",
"data": {
"items": data,
"total": data.length
}
});
});
app.listen(8000, () => {
console.log('Listening on port 8000');
});
The access result is as follows:
Is there a problem with the results returned by the API server, causing the form to be unable to read data? Or which step is configured? Thanks!