【发布时间】:2018-10-05 08:58:27
【问题描述】:
所以我已经尝试了所有方法,但我无法弄清楚为什么 tumblr 对象没有通过 res.send 到我的前端。任何人都可以帮忙吗?我也尝试了 res.json ,但这没有任何作用。我收到了一个承诺错误,一个异常被捕获,但我不确定为什么。我认为它与未通过的图像对象有关。
import React from 'react';
import axios from 'axios';
class SearchBar extends React.Component {
constructor(props) {
super(props);
this.state = {tag: "", images: ''};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleChange(event){
this.setState({tag: event.target.value});
}
handleSubmit(event) {
event.preventDefault();
const sendTag = () => (
new Promise((resolve, reject) => {
axios.post('/tag', {
tag: this.state.tag,
})
if(this.state.tag){
resolve(console.log('in sendtag'))
} else {
reject (error)
}
}))
.then(() => {
axios.post('/images')
.then(res => {
console.log("AXIOS:", res)
this.setState({images: res})
})
})
.then((res) => {
console.log('IMAGES:', this.state);
})
.catch((error) => {
console.log(error);
});
sendTag()
}
render (){
return (
<div>
<form onSubmit={this.handleSubmit}>
<input type="text"
onChange={this.handleChange}
className="searchTerm"
placeholder="Enter a tag" />
<input type="submit" value="Submit" />
</form>
<div className="grid-container">
</div>
</div>
)
}
}
export default SearchBar;
服务器.js
//Express
const express = require('express');
const app = express();
const path = require('path');
const bodyParser = require('body-parser');
const port = process.env.PORT || 5000;
//TUMBLR I
const tumblr = require('tumblr.js');
//TOKENS
const secret = require('./secret/config.js');
//MIDDLEWARE
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, '../frontend/public')));
//INITIATE API
const client = tumblr.createClient({
consumer_key: secret.consumer_key,
consumer_secret: secret.consumer_secret,
token: secret.token,
token_secret: secret.token_secret
});
// API
new Promise(function(resolve, reject){
app.post('/tag', (req, res) =>{
const tag = req.body.tag
if(tag){
resolve(tag)
} else {
reject (error)
}
})
}).then(function(tag){
return new Promise(function(resolve, reject){
console.log('TAG', tag)
console.log('tumble api')
client.taggedPosts(tag, (error, data) => {
if(data){
console.log('data recieved')
resolve(data)
} else {
reject (error)
console.log('tumble api error')
}
})
});
}).then(function(result){
return new Promise(function(resolve, reject){
console.log('image to new variable')
const images = result
if (images){
resolve (images)
} else {
reject (error)
}
})
}).then(function(images){
console.log('send api')
console.log('IMAGES', images)
app.post('/images', (req, res) => {
res.json(images)
})
})
// FRONTEND ROUTE
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, '../frontend/index.html'));
});
//SERVER
app.listen(port, () => console.log('Server running on localhost 5000!'));
module.exports = app;
【问题讨论】:
-
日志打印是否正确?您是否尝试过使用邮递员等其他客户端工具验证 REST API?
-
process.on('unhandledRejection', function onError(err) {console.error(err);});把它放在你的代码中,尝试运行应用程序,它会显示错误...
-
所以,我添加了代码,现在数据显示在 post 对象中,但事实并非如此。谢谢你给我一个起点!
-
抱歉没能找到您...您的意思是图像正在打印在日志上或在休息响应中。 ?
-
所以现在当我从我的 ajax 控制台记录结果时,图像显示在我前端的对象中。数据以前不存在,但现在出现了。
标签: javascript reactjs webpack jsx