小编Kev*_*Lee的帖子

如何检查axios通话是否因无网络而失败?

我正在尝试找出一种准确的方法来检测由于没有互联网连接而导致的 axios 呼叫失败。

axios 调用失败是否返回特定的响应对象或特定的响应状态代码或针对无互联网连接场景抛出特定的错误详细信息?

我将使用“检测由于没有互联网连接而导致的 axios 故障”的示例

try {
    const res = await server.get('/auth/user?', {
            params: {
                refreshToken: refreshToken,            
                userID: userID
            }
        }
    );

    if(res.user.data){
        dispatch({type: LOGIN_USER_SUCCESS, payload: res.data.user});
    } else {
        if(res.axiosFailsDueToNoInternetConnection){
            alert('no internet connection');
            dispatch({type: RELOAD});
        }
    }

} catch (error) {
    if(error.dueToNoInternetConnection){
        alert('no internet connection');
        dispatch({type: RELOAD});
    }
}
Run Code Online (Sandbox Code Playgroud)

networking reactjs server react-native axios

7
推荐指数
1
解决办法
1万
查看次数

在 React Native 中将持久会话的访问令牌设置为 redux 状态是否安全?

我正在尝试找出一种安全的方法来在本机反应中保留会话。

我有一些敏感数据,例如从服务器检索的访问令牌,我计划将其设置为 redux 状态。

我不确定设置敏感数据是否安全accessTokenredux 状态)是否安全。

如果不安全,我是否应该将其保存accessToken到设备存储(例如react-native-keychain)accessToken并在每个屏幕请求和服务器请求上加载?

dispatch({type: LOGIN_SUCCESS, payload: {refreshToken, authTimestamp, email} });
Run Code Online (Sandbox Code Playgroud)
const INITIAL_STATE = {    
    token: {      
        accessToken: '',          
        loading: false,
        error: ''
    }
};
Run Code Online (Sandbox Code Playgroud)

react-native redux

7
推荐指数
1
解决办法
2120
查看次数

由于需要更新通知程序包,nodemon未运行

当我在终端中键入"nodemon server.js"命令时,它返回错误"require('update-notifier')({pkg}).notify();".安装的nodemon版本是nodemon@1.17.3

在此输入图像描述

以下是使用的javascript和html.

var express = require('express');
var app = express();
var port = 8888;

app.get('/', function(req, res, next) {
  res.sendFile(__dirname + '/index.html');
});

app.listen(port, '0.0.0.0', function() {
  console.log('Server running at port ' + port);
});
Run Code Online (Sandbox Code Playgroud)
<!DOCTYPE html>
<html>

<head>
  <title>My NodeJS Website</title>
</head>

<body>
  <p>Hello World!</p>
</body>

</html>
Run Code Online (Sandbox Code Playgroud)

node.js nodemon

6
推荐指数
1
解决办法
9603
查看次数

如何在 React Native 的 Podfile 中结合安装后安装?

我需要将以下安装后添加到 Podfile。

post_install do |pi|
  pi.pods_project.targets.each do |t|
    t.build_configurations.each do |config|
      config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '9.0'
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

但是遇到了这个错误。

[!] Invalid `Podfile` file: [!] Specifying multiple `post_install` hooks is unsupported..
Run Code Online (Sandbox Code Playgroud)

Podfile 中有两个安装后。我应该如何结合它们来解决错误?

post_install do |installer|
    flipper_post_install(installer)
  end
end

post_install do |pi|
  pi.pods_project.targets.each do |t|
    t.build_configurations.each do |config|
      config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '9.0'
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

ios cocoapods reactjs react-native react-native-ios

5
推荐指数
1
解决办法
1888
查看次数

如何使用express-validator验证正则表达式?

我正在尝试使用express-validator验证手机号码正则表达式。

我浏览了文档 api,但找不到检查正则表达式的验证器函数。regex.test()像下面这样使用会起作用吗?


body('mobile', 'Invalid mobile number.')
           .escape()
           .exists({checkFalsy: true})
           .isLength({min: 11, max:11})
           .regex.test(/^(\+123)\d{11}$/)
Run Code Online (Sandbox Code Playgroud)

validation express-validator

4
推荐指数
1
解决办法
1万
查看次数

如何向猫鼬模式子字段添加时间戳?

我正在尝试在in和的子字段中添加createdAtupdatedAt时间戳otpgenerate: {}verify:{}

我知道使用会将和时间戳{ timestamps: true }添加到整个架构中。createdAtupdatedAt

const userSchema = new mongoose.Schema({    
    email: { type: String, unique: true },
    name: { type: String }, 
    mobileNumber: {
        isVerified: {type: Boolean, default: false},
        otp: {
            generate: {
              attempts: {type: Number, default: 0},
              total: {type: Number, default: 0},
              createdAt: {type: Date},
              updatedAt: {type: Date}
            },
            verify: {
              attempts: {type: Number, default: 0},
              total: {type: Number, default: 0},
              createdAt: {type: Date},
              updatedAt: {type: Date} …
Run Code Online (Sandbox Code Playgroud)

mongoose mongodb mongoose-schema

3
推荐指数
1
解决办法
3798
查看次数

MongooseError:在新的 MongooseError 处保存之前,文档必须有一个 _id

我有一个注册页面,需要输入用户名和密码才能存储到 mongoDB 中。 注册画面

当我单击“注册”按钮时,它会加载一个错误页面。 注册错误

这也会在终端中导致 Mongoose 错误,显示 MongooseError: document must have an _id before save at new MongooseError

在此处输入图片说明

下面是代码。

//server.js file
var express = require('express');
var app = express();
var port = 8888;
var mongoose = require('mongoose');
var bodyParser = require('body-parser');

/*Body parser*/
app.use(bodyParser.urlencoded({
    extended: true
}));

/*Database connection - MongoDB*/

//Created from the command earlier. Ensure this is done on the first_db instance
var username = 'admin';
var password = '123456';

var dbHost = 'localhost';
var dbPort = '27017'; …
Run Code Online (Sandbox Code Playgroud)

mongoose mongodb node.js

2
推荐指数
1
解决办法
4885
查看次数

如何使用 React Navigation 5 导航到 Action Creators 内的屏幕

React Navigation 5 Auth Flow中,它表示当条件状态发生变化时屏幕将自动导航。

  • 我已将屏幕设置为当
    isAuthenticated 的状态更改为 true 时导航到 HomeScreen。
  • 控制台中没有错误。isAuthenticated 确实更改为 true,但状态更改后屏幕未导航到主屏幕。
  • 我还尝试在操作创建器中使用 {NavigationActions} 和 {CommonActions} 等替代方案。强制导航,但也不起作用。

AuthStackNav.js

import React from 'react';
import { connect } from 'react-redux';
import {createStackNavigator} from '@react-navigation/stack';
import AuthScreen from '../screens/AuthScreen';
import HomeScreen from '../screens/HomeScreen';

const AuthStackNav = ({isAuthenticated}) => {
    const AuthStack = createStackNavigator();

    return (        
        <AuthStack.Navigator initialRouteName='Auth'>
        {
            isAuthenticated ?
            <AuthStack.Screen name='Home' component={HomeScreen} />                
            :
            <AuthStack.Screen  name='Auth' component={AuthScreen} />      
        }                       
                         
        </AuthStack.Navigator>
        
    );
};
const mapStateToProps = ({isAuthenticated}) => { …
Run Code Online (Sandbox Code Playgroud)

reactjs react-native react-redux react-navigation react-navigation-v5

2
推荐指数
1
解决办法
2563
查看次数

如何从 axios.create({baseURL}) 获取 BaseURL?

customApi.js我正在尝试从使用配置创建的 axios获取 baseURL export default axios.create()

自定义Api.js 文件

import axios from 'axios';

export default axios.create({
    baseURL: 'http://123.123.123.123:5000'
});
Run Code Online (Sandbox Code Playgroud)

我尝试了以下操作,但它控制台记录了undefined

import api from 'customApi.js';

console.log(api.baseURL);
Run Code Online (Sandbox Code Playgroud)

在自定义配置的 axios 中获取基本 url 的正确方法是什么?

javascript networking axios

2
推荐指数
1
解决办法
3956
查看次数

如何在 React-Native React Navigation 5 中向下滚动时制作从透明到不透明颜色的动画标题?

我正在尝试制作标题,在使用 React-Native React Navigation 5向下滚动时,该标题将从透明变为纯不透明颜色。

滚动到一半时开始过渡到不透明

在此处输入图片说明


达到最大偏移量时变得完全不透明

在此处输入图片说明

reactjs react-native react-navigation react-navigation-v5

1
推荐指数
1
解决办法
2056
查看次数