这就是我的目标:
import 'whatwg-fetch';
function fetchVehicle(id) {
return dispatch => {
return dispatch({
type: 'FETCH_VEHICLE',
payload: fetch(`http://swapi.co/api/vehicles/${id}/`)
.then(status)
.then(res => res.json())
.catch(error => {
throw(error);
})
});
};
}
function status(res) {
if (!res.ok) {
return Promise.reject()
}
return res;
}
Run Code Online (Sandbox Code Playgroud)
编辑:承诺不会被拒绝,这就是我想要弄清楚的.
我在Redux中使用这个fetch polyfill和redux-promise-middleware.
你将如何实现类似Array unshift()方法的东西,以便创建一个新的数组?基本上类似于Array concat()方法,但不是将新项目放在最后,而是将其放在开头.
我已经阅读了3次文档,但我仍然不知道它的作用.有人可以ELI5(解释就像我五岁)吗?这是我使用它的方式:
fun main(args: Array<String>) {
val UserModel = UserModel()
val app = Javalin.create().port(7000).start()
with (app) {
get("/users") {
context -> context.json(UserModel)
}
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个应用程序,客户端通过https与Nginx从example.com发出多部分请求到api.example.com,然后api将文件上传到Amazon S3.
它适用于我的机器,但当其他人在不同的网络上尝试时会中断.给我这个错误:
[Error] Origin https://example.com is not allowed by Access-Control-Allow-Origin.
[Error] Failed to load resource: Origin https://example.com is not allowed by Access-Control-Allow-Origin. (graphql, line 0)
[Error] Fetch API cannot load https://api.example.com/graphql. Origin https://example.com is not allowed by Access-Control-Allow-Origin.
Run Code Online (Sandbox Code Playgroud)
我在API上使用cors npm包,如下所示:
app.use(cors());
Run Code Online (Sandbox Code Playgroud)
所有这些都是通过DigitalOcean上的Nginx反向代理进行的.这是我的Nginx配置:
个人服务器配置/etc/nginx/conf.d/example.com.conf和/etc/nginx/conf.d/api.example.com.conf几乎完全相同的地址和名称不同:
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
include snippets/ssl-params.conf;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://localhost:3000/; …Run Code Online (Sandbox Code Playgroud) 试图导入此文件:
subscription onNewNotification {
notification {
id
content
seen
}
}
Run Code Online (Sandbox Code Playgroud)
运行babel-node编译整个事情时出现此错误: SyntaxError: .../graphql/NotificationSubscription.graphql: Unexpected token, expected ;
这就是我的webpack加载器的样子:
module: {
loaders: [
{
test: /\.js?$/,
include: [
path.join(__dirname, 'client'),
path.join(__dirname, 'server/shared'),
],
exclude: /node_modules/,
loader: 'babel-loader',
query: {
presets: ['react-hmre'],
},
},
{
test: /\.json?$/,
loader: 'json-loader',
},
{
test: /\.scss$/,
loaders: ['style-loader', 'css-loader', 'sass-loader'],
},
{ test: /\.css$/, loader: 'style-loader!css-loader' },
{ test: /\.svg(\?v=\d+\.\d+\.\d+)?$/, loader: 'file-loader?mimetype=image/svg+xml' },
{
test: /\.(graphql|gql)$/,
exclude: /node_modules/,
loader: 'graphql-tag/loader',
},
],
},
Run Code Online (Sandbox Code Playgroud)
不知道为什么会发生这种情况,我正在关注Apollo文档,我在谷歌上没有看到任何关于此的内容: …
我想从本地网络上的设备测试React网站.它适用于其他PC,但不适用于我的手机.
你们有什么想法会导致这个吗?这是我的配置文件的外观:
const webpack = require('webpack');
const path = require('path');
module.exports = {
entry: {
mainFeedPage: [
'webpack/hot/only-dev-server',
'./src/mainFeedPage.js'
],
venues: [
'webpack/hot/only-dev-server',
'./src/venues.js'
],
artists: [
'webpack/hot/only-dev-server',
'./src/artists.js'
]
},
output: {
path: path.resolve(__dirname, 'public'),
filename: 'js/[name].js',
publicPath: '/public/'
},
devServer: {
inline:true,
port: 4000,
hot: true,
colors: true,
progress: true,
host: '0.0.0.0'
},
module: {
loaders: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
loaders: [
'react-hot',
'babel?presets[]=react,presets[]=es2015'
]
}
]
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new webpack.NoErrorsPlugin(),
new webpack.optimize.CommonsChunkPlugin({ …Run Code Online (Sandbox Code Playgroud) 我有一个客户端服务器和一个API,都使用Apollo。当用户使用Facebook登录时,我想将令牌发送到客户端服务器,然后将其附加到对API进行的每个请求的标头中。不使用会话。
API的外观如下,当用户使用Facebook登录时,此API会成功生成令牌(我也在后台检查数据库中的用户)。
// API running on port 3010
app.use(expressJwt({
secret: authConfig.jwt.secret,
credentialsRequired: false,
getToken: req => req.cookies.id_token,
}));
app.use(passport.initialize());
app.get('/login/facebook',
passport.authenticate('facebook', { scope: ['email'], session: false }),
);
app.get('/login/facebook/return',
passport.authenticate('facebook', { failureRedirect: 'http://localhost:3000/login', session: false }),
(req, res) => {
const expiresIn = 60 * 60 * 24 * 1; // 1 day
const token = jwt.sign(req.user, authConfig.jwt.secret, { expiresIn });
res.redirect(`http://localhost:3000?token=${token}`); // Redirect to client and append token to param
},
);
Run Code Online (Sandbox Code Playgroud)
我的问题是,现在如何安全地将此令牌交给客户端?我已经读过这个和this,这使我不得不在参数中传递令牌,但是我不确定我在做什么是否安全。 …
我正在从Webpack迁移到Parcel,并且在组件中使用了以下文件夹别名:
import * as authActions from 'actions/authActions';
Run Code Online (Sandbox Code Playgroud)
我收到此错误: Cannot find module 'actions/authActions'
奇怪的是,它仅在使用时显示build,它在开发模式下有效,而在生产模式下则无效。
我已经在package.json中设置了我的别名,如文档所示:
{
"scripts: {
"build-client": "parcel build client/index.html dist/client",
"build-server": "parcel build server/index.js --target node -d dist/server",
"build-test": "yarn build-server && node ./dist/server/index.js"
},
"alias": {
"actions": "./client/actions"
}
}
Run Code Online (Sandbox Code Playgroud)
这是服务器端渲染的应用程序,我将组件导入到其他位置,并且无法使用默认的Parcel根目录,因为它是相对于输入文件的。
如何获得此名称以正确解析别名?
我有使用参数的路线,我想渲染/重定向到404路线,如果一个动作返回从没有数据的外部API.这样做最明智的方法是什么?
这是路由器:
export default (
<Route path="/" component={App}>
<Route path=":venueName" component={Venue} />
<Route path="*" component={NotFound} />
</Route>
);
Run Code Online (Sandbox Code Playgroud)
Venue组件连接到Redux并触发一个动作:
// ...
componentWillMount() {
this.props.actions.fetchVenue(this.props.id);
// want to redirect to 404 if this returns success === false
}
// ...
Run Code Online (Sandbox Code Playgroud)
动作看起来像这样:
export const fetchVenue = (id) => ({
type: 'FETCH_VENUE',
payload: axios.get('http://someAddress/api/venues/id'),
});
Run Code Online (Sandbox Code Playgroud)
我在这里遇到的问题是如何通过服务器端渲染和任何数量的路由来解决这个问题.我在express中使用来自react-router的"匹配"来呈现标记.如果您希望我发布更多代码,请告诉我.
我正在使用react-dropzone和graphql-server-express-upload在GraphQL Apollo中上传所有图像.在我的客户端中,文件如下所示:
但是在服务器中,当我记录它时,它看起来像这样:
{ preview: 'blob:http://localhost:3000/c622b0bd-3f0d-4f52-91f4-7676c8534c59' }
Run Code Online (Sandbox Code Playgroud)
我正在按照graphql-server-express-upload中的自述文件中的说明进行操作,但到目前为止还没有运气.如何获得完整图像?
我正在通过标签功能进行搜索,在这样的表中
CREATE TABLE permission (
id serial primary key,
tags varchar(255)[],
);
Run Code Online (Sandbox Code Playgroud)
然后我添加一行具有“艺术家”和“默认”标签的行。
我想通过标签查询它(使用knex 查询生成器),所以如果我这样做:
async getByTags(tags: string[]): Promise<PermissionTable[]> {
return this.db<PermissionTable>('permission')
.select('*')
.whereRaw("tags @> '{??}'", [tags])
}
Run Code Online (Sandbox Code Playgroud)
这会根据您传递的标签数量生成以下语句。
这有效
async getByTags(tags: string[]): Promise<PermissionTable[]> {
return this.db<PermissionTable>('permission')
.select('*')
.whereRaw("tags @> '{??}'", [tags])
}
Run Code Online (Sandbox Code Playgroud)
这不会(返回一个空数组,而它应该是我正在查找的行)
select * from "permission" where tags @> '{"artist"}';
Run Code Online (Sandbox Code Playgroud)
为什么有多个标签的不起作用?
javascript ×9
graphql ×3
node.js ×3
reactjs ×3
redux ×2
webpack ×2
arrays ×1
cors ×1
fetch-api ×1
knex.js ×1
kotlin ×1
nginx ×1
parceljs ×1
postgresql ×1
react-apollo ×1
react-router ×1
sql ×1
typescript ×1