[已解决] koa 不能发起 get 请求
来源:11-18 视频列表页对接后台数据
sai1024
2017-10-30
// 客户端代码 componentDidMount() { var that = this AsyncStorage.getItem('user') .then((data) => { var user if (data) { user = JSON.parse(data) } if (user && user.accessToken) { that.setState({ user: user }), function() { that._fetchData(1) } } }) }, _fetchData(page){ var that = this if (page !== 0) { this.setState({ isLoadingTail: true }) } else { this.setState({ isRefreshing: true }) } var user = this.state.user request.get(config.api.base + config.api.creations, { accessToken: user.accessToken, page: page }) .then((data) => { console.log(data) if(data && data.success) { if (data.data.length > 0) { var items = cachedResults.items.slice() if (page !== 0) { items = items.concat(data.data) cachedResults.nextPage += 1 } else { items = data.data.concat(items) } cachedResults.items = items cachedResults.total = data.total if (page !== 0) { that.setState({ isLoadingTail: false, dataSource: that.state.dataSource.cloneWithRows(cachedResults.items) }) } else { that.setState({ isRefreshing: false, dataSource: that.state.dataSource.cloneWithRows(cachedResults.items) }) } } } }) .catch((error) => { if (page !== 0) { this.setState({ isLoadingTail: false }) } else { this.setState({ isRefreshing: false }) } }); },
## 服务端主要代码:
router.get('/creations', App.hasToken, Creation.find)
exports.find = function *(next) { var page = parseInt(this.query.page, 10) || 1 var count = 5 var offset = (page - 1) * count var queryArray = [ Creation .find({finish: 100}) .sort({ 'meta.createAt': -1 }) .skit(offset) .limit(count) .populate('author', userFields.join(' ')) .exec(), Creation.count({finish: 100}).exec() ] var data = yield queryArray console.log(data) this.body = { success: true, data: data[0], total: data[1] } }
## 版本
{ "name": "imooc-server", "version": "1.0.0", "description": "rn app for imooc", "main": "app.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "Brave", "license": "MIT", "dependencies": { "bluebird": "^3.4.1", "cloudinary": "^1.9.0", "koa": "^1.2.0", "koa-bodyparser": "^2.2.0", "koa-logger": "^1.3.0", "koa-router": "^5.4.0", "koa-session": "^3.3.1", "lodash": "^4.13.1", "mongoose": "^4.5.3", "qiniu": "^6.1.11", "sha1": "^1.1.1", "speakeasy": "^2.0.0", "uuid": "^2.0.2", "xss": "^0.2.13" } }
命令行里什么都不输出
请老师帮忙看一看
写回答
2回答
-
Scott
2017-11-01
你的 koa 版本多少的,是 2.x 么
如果是的话,把这块的语法改一下吧
exports.find = async function (ctx, next) { var page = parseInt(ctx.query.page, 10) || 1 var count = 5 var offset = (page - 1) * count var queryArray = [ Creation .find({finish: 100}) .sort({ 'meta.createAt': -1 }) .skit(offset) .limit(count) .populate('author', userFields.join(' ')) .exec(), Creation.count({finish: 100}).exec() ] var data = await Promise.all(queryArray) console.log(data) ctx.body = { success: true, data: data[0], total: data[1] } }
122017-11-11 -
sai1024
提问者
2017-11-11
if (user && user.accessToken) { that.setState({ user: user }), function() { that._fetchData(1) }
。。。之前一直无法调试,今天弄的终于可以调试了,然后发现 _fetchData() 这个方法压根没调用,最后发现是因为 function() 前面的括号加错了,应该是在最后面。
下面是正确的写法:
if (user && user.accessToken) { that.setState({ user: user }, function() { that._fetchData(1) })
00
相似问题