小编Geo*_*off的帖子

将String转换为int.如果String为null,则将int设置为0

我有一个保存Android数据的功能,sqlite但我必须将String数据转换为Integer.

只要Stringnull我想保存为0

以下是我的代码,只要值是,它就会失败 null

 int block_id = Integer.parseInt(jsonarray.getJSONObject(i).getString("block_id"));
Run Code Online (Sandbox Code Playgroud)

block_id上述转化为Integer.

这是我决定要做的,但它仍然无法将字符串值转换为0它的时间null.

int block_id = Converttoint(jsonarray.getJSONObject(i).getString("block_id"));
Run Code Online (Sandbox Code Playgroud)

然后功能 convertToInt

 public static Integer convertToInt(String str) {
    int n=0;
  if(str != null) {
      n = Integer.parseInt(str);
  }
    return n;
}
Run Code Online (Sandbox Code Playgroud)

我应该如何改变它,让它工作?

java android

23
推荐指数
7
解决办法
2810
查看次数

更改axios的默认基本URL

我已经这样配置了我的axios

const axiosConfig = {
baseURL: 'http://127.0.0.1:8000/api',
timeout: 30000,
};

Vue.prototype.$axios = axios.create(axiosConfig)
Run Code Online (Sandbox Code Playgroud)

在我的组件内部,我打电话给

this.$axios.get('items').then()..
Run Code Online (Sandbox Code Playgroud)

现在上面的方法可以了,但是我想在不影响全局基本URL的情况下更改baseURL,以便在我的组件中我可以简单地使用而无需api端点,因此

我试过了

this.$axios.baseURL = "http://127.0.0.1:8000;
this.$axios.get()..//this is still in api endpoint
Run Code Online (Sandbox Code Playgroud)

我该怎么办?

javascript vuejs2

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

Yii2 cors过滤错误,表示没有'Access-Control-Allow-Origin'标头

以下这个问题我将我的休息控制器行为设置为

public function behaviors()
{
    $behaviors = parent::behaviors();

    $auth= $behaviors['authenticator'] = [
        'class' => HttpBearerAuth::className(),
        'only' => ['dashboard'],
    ];
    $behaviors['contentNegotiator'] = [
        'class' => ContentNegotiator::className(),
        'formats' => [
            'application/json' => Response::FORMAT_JSON,
        ],
    ];
    $acces=$behaviors['access'] = [
        'class' => AccessControl::className(),
        'only' => ['login'],
        'rules' => [
            [
                'actions' => ['login'],
                'allow' => true,
                'roles' => ['?'],
            ],
        ],
    ];

    unset($behaviors['authenticator']);
    unset($behaviors['access']);
Run Code Online (Sandbox Code Playgroud)

现在,cors过滤器

    // add CORS filter
    $behaviors['corsFilter'] = [
        'class' => \yii\filters\Cors::className(),
          'cors' => [
        // restrict access to …
Run Code Online (Sandbox Code Playgroud)

php yii2 yii2-advanced-app angular

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

在redis中存储多个嵌套对象

我想在redis中存储多个复杂的json数据,但我不确定如何

这是我的json结构

"users":{

        "user01":{username:"ally", email:"all@gmail.com"},
         "user02":{username:"user2".....}
        },

 "trucks":{
         "truck01":{reg_no:"azn102", make:"subaru" .......},
         "truck02":{reg_no:"kcaher3".....}
       }
Run Code Online (Sandbox Code Playgroud)

我已经检查了 这个提供存储用户的方法的问题,但我想将用户(01,02)存储在用户内,然后将卡车(01,02)存储在卡车中,这样如果我想要检索用户,我可以简单地做

hmget users
Run Code Online (Sandbox Code Playgroud)

和卡车类似的情况

后来我也想在用户中获取user01,但对如何在redis中存储这些数据感到困惑

json redis

15
推荐指数
4
解决办法
7471
查看次数

指定jest test files目录

我是单元测试的新手,我只想测试位于特定目录中的文件

我如何指定我希望测试仅在特定目录上运行到文件而忽略其他目录

所以我通过npm安装了jest

  "jest": "^23.6.0",
Run Code Online (Sandbox Code Playgroud)

并在package.json中指定了我的测试命令

scripts:{
    "test": "jest --verbose"
 }
Run Code Online (Sandbox Code Playgroud)

上面运行所有文件,但我希望它运行在特定目录中的文件,例如最新目录

我该怎么办?

javascript jestjs

13
推荐指数
5
解决办法
6270
查看次数

Vuejs仅在加载数据后才挂载子组件

我想要实现的是在我的子组件中将数据作为道具传递,但是这些数据是从服务器加载的,因此加载需要一段时间.

我现在只想在数据完全加载时挂载子组件

所以目前正在这样做

在父组件中

<template>
  <child-cmp :value="childdata"></child-cmp>
</template>

<script>
  export default{
    data(){
       childdata : [];
     },

     methods:{
      fetchINitData(){
        //fetch from server then
         this.childdata = res.data.items;
         console.log(res.data.items) //has some values

       }

     }


   components:{
     childcmp
    },

   mounted(){
     this.fetchINitData();


     }
    }
 </script>
Run Code Online (Sandbox Code Playgroud)

我的孩子组件中没有

<script>
export default{
   props:["value];

    mounted(){
      console.log(this.value) //this is always empty
     } 

     }

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

从上面的示例中,作为props传递的数据在子组件上始终为空.如何在收到数据后才挂载子组件,或者如何确保子组件获取最新的数据更改.

javascript vuejs2

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

在yii2中传递持有者令牌会返回错误401

我使用angular2和yii2 restful服务,但它失败了

这是我的角色2部分

In the userservice
 constructor(private http:HttpClientService, private _domaindetails:Domaindetails) { }

profile(val:string="profile"):Observable<User>{
return this.http.get(this.getUserUrl(val))
  .map(this.extractData)

 }
Run Code Online (Sandbox Code Playgroud)

这是httpclientService

 createAuthorizationHeader(headers: Headers) {
  let token = JSON.parse(localStorage.getItem("currentUser")).token;
  if (token) {
    headers.append('Authorization', 'Bearer ' + token);
    headers.append('Content-Type', 'application/x-www-form-urlencoded');
    headers.append('Content-Type', 'application/json');
   }

  }

post(url, data) {
  let headers = new Headers();
  this.createAuthorizationHeader(headers);
 return this.http.post(url, data, {
    headers: headers
 });
}
Run Code Online (Sandbox Code Playgroud)

这是我在Yii2的后端

    public function behaviors()
{
    $behaviors = parent::behaviors();
     $behaviors['corsFilter'] = [
        'class' => \yii\filters\Cors::className(),
        'cors' => [
            // restrict access to
            'Origin' => ['http://localhost:4200'], …
Run Code Online (Sandbox Code Playgroud)

php rest yii2 typescript2.0 angular

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

在laravel eloquent创建中保存多个记录

我正试图通过保存多个记录

AppSettings::create(
    [
        'name'=>'mail_host',
        'type'=>$emailsettingstype->id,
        'value'=>'',

    ],
    [
        'name'=>'mail_port',
        'type'=>$emailsettingstype->id,
        'value'=>'',

    ],
    [
        'name'=>'mail_username',
        'type'=>$emailsettingstype->id,
        'value'=>'',
    ],
);
Run Code Online (Sandbox Code Playgroud)

但是从上面开始,只创建了第一个数组.我哪里错了?任何帮助表示赞赏.

php laravel

8
推荐指数
4
解决办法
8175
查看次数

Flutter getx 控制器获取当前页面上下文

我想使用上下文在 getxcontroller 方法中显示来自酷警报的自定义对话框。我创建了以下控制器

class HomePageController extends GetxController {
 
   @override
   void onInit() {
     super.onInit();
     getData();
   }

   void getData(){
    //perform http request here 
     //show cool alert 

     CoolAlert.show(
      context: context,  //here needs the build context
      type: CoolAlertType.success
      );
   }

}
Run Code Online (Sandbox Code Playgroud)

我在我的无状态小部件中使用这个控制器,例如

class HomePage extends StatelessWidget {
   HomePage({ Key? key }) : super(key: key);

   final _c = Get.find<HomePageController>();


    @override
    Widget build(BuildContext context) {
        return Container(
  
          );
    }
 }
Run Code Online (Sandbox Code Playgroud)

我如何在控制器中获取当前主页 BuildContext 以显示酷警报。

flutter flutter-getx

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

React 路由器路由加载器不适用于嵌套组件

我正在使用 React Router v6,我想在组件加载之前使用新的加载器来加载数据。所以我有以下内容

在我的index.js中

const router = createBrowserRouter(
createRoutesFromElements(
    <Route path="*"
           loader={async ({ params }) => {

               console.log("index loader log"); //this logs

               return true;
           }}
           element={<App />}
     > </Route>
  )
);
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
  <RouterProvider router={router} />
);
Run Code Online (Sandbox Code Playgroud)

在我的应用程序组件中,我有嵌套的路由,例如

const App = () => {
 return (
     <>
        <Routes>
            <Route path="auth/*" element={<AuthLayout/>}/>
            <Route path="about"
             loader={async ({ params }) => {

               console.log("about child loader log"); //this doesnt log

               return true;
              }}
           element={<AboutPage/>}/>
        </Routes>
        <h1>Testing app</h1>
     </>

   );

 } …
Run Code Online (Sandbox Code Playgroud)

reactjs react-router react-router-dom

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