小编phw*_*hwt的帖子

Next.js API 在出现错误或没有 HTTP 方法匹配时路由默认响应

我正在 Next.js 中处理API 路由,每个路径的结构如下:

import { NextApiRequest, NextApiResponse } from "next";

export default async (req: NextApiRequest, res: NextApiResponse) => {
  const { query } = req;

  try {
    switch (req.method) {
      case "GET":
        // Do some GET stuff
        res.status(200).send(result);
        break;

      case "POST":
        // Do some POST stuff
        res.status(200).send(result);
        break;

      default:
        // No Matched Method
        res.status(405).send("Method Not Allowed");
        break;
    }
  } catch (e) {
    // Unprocessable Entity
    res.status(422).send(e);
  }
};
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,default案例和catch零件将在所有文件中重复。所以,我想知道:

  • 当没有匹配的方法或处理请求时出现错误时,Next.js 是否有内置支持默认响应?
  • (如果没有内置支持)怎么办?

如果没有内置支持,这是我的解决方案: …

javascript node.js express next.js

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

process.env 中的用户定义键在 JavaScript Azure Functions 中未定义

我正在将 HTTP 触发器函数部署到 Azure Function,但是当我尝试访问其中的条目时,process.env它会返回undefined. 它可以在本地模拟器中运行,但在部署到 Azure 上时不能运行。


测试在本地和 Azure 上运行该函数。

我已经用一个非常简单的 HTTP 函数对其进行了测试。

import { AzureFunction, Context, HttpRequest } from "@azure/functions";

const httpTrigger: AzureFunction = async function (
  context: Context,
  req: HttpRequest
): Promise<void> {
  context.log(process.env.GCP_SERVICE_ACCOUNT_EMAIL);

  context.res = {
    body: process.env.GCP_SERVICE_ACCOUNT_EMAIL,
  };
};

export default httpTrigger;
Run Code Online (Sandbox Code Playgroud)

本地运行

当本地运行时,它会输出值设置local.settings.json

[2021-09-11T13:41:27.737Z] Executing 'Functions.HttpTrigger' (Reason='This function was programmatically called via the host APIs.', Id=b9a303aa-5d14-403c-b841-4851f00beb65)
[2021-09-11T13:41:27.768Z] service_account@local.settings.json
[2021-09-11T13:41:27.848Z] Executed 'Functions.HttpTrigger' (Succeeded, Id=b9a303aa-5d14-403c-b841-4851f00beb65, Duration=126ms)
Run Code Online (Sandbox Code Playgroud)

在 Azure 上运行

但是,当在 …

javascript azure node.js terraform azure-functions

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

如何以编程方式更改Android菜单

我有两种不同的菜单布局.我想在单击按钮时更改运行时的膨胀菜单.

最初菜单设置为@menu/navigation.我希望它更改@menu/navigation2为单击按钮时.

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="th.ac.sd.sdschoolas.MainActivity">

    <FrameLayout
        android:id="@+id/content_main"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <WebView
            android:id="@+id/web_main"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="152dp"
            android:layout_height="158dp"
            android:layout_x="23dp"
            android:layout_y="46dp"
            app:srcCompat="@mipmap/logo"
            android:padding="20dp"/>
    </FrameLayout>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="?android:attr/windowBackground"
        app:menu="@menu/navigation" />

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

菜单/ navigation.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/nav1"
        android:icon="@mipmap/homeicon"
        android:title="@string/title_home" />

    <item
        android:id="@+id/nav2"
        android:icon="@mipmap/newsicon01"
        android:title="@string/title_news" />

    <item
        android:id="@+id/nav3"
        android:icon="@drawable/ic_calendar_page_empty"
        android:title="@string/title_cal" />

    <item
        android:id="@+id/nav4"
        android:icon="@drawable/ic_chat_bubbles"
        android:title="@string/title_sms" />

    <item
        android:id="@+id/nav5"
        android:icon="@mipmap/more"
        android:title="@string/title_more" />

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

菜单/ …

android android-menu

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

Angular 2+ - 将 JSON 解析为数字

我想使用Angular Google Maps在 Google Maps 上放置一个标记,我的坐标来自 JSON 文件,当在 Angular 中解析它时,它变成了一个字符串。

Angular Google Maps 不支持坐标数字字符串。所以它需要是数字。

标记.json

[  
   {  
      "id":"1",
      "lat":"13.751814",
      "lon":"100.501060"
   },
   {  
      "id":"2",
      "lat":"13.738445",
      "lon":"100.516805"
   },
   {  
      "id":"3",
      "lat":"13.730209",
      "lon":"100.779991"
   }
]
Run Code Online (Sandbox Code Playgroud)

地图.componentnet.ts

import { Component, OnInit } from '@angular/core';
import {Http, Response} from '@angular/http';

@Component({
  selector: 'app-maps',
  templateUrl: './maps.component.html',
  styleUrls: ['./maps.component.css']
})
export class MapsComponent implements OnInit {

    private data;

    constructor(private http:Http){
    }

    ngOnInit(){
      this.getData();
    }

    getData(){
        this.http.get('/localhost/marker.json')
            .subscribe(res => this.data = res.json());
    }

}
Run Code Online (Sandbox Code Playgroud)

地图.component.html

<agm-map …
Run Code Online (Sandbox Code Playgroud)

json typescript angular

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

Android Studio 在片段中打开抽屉

我从应用程序中删除了操作栏,所以我需要创建按钮来自己打开抽屉 Activity 上的按钮工作正常但是当我在片段应用程序中使用它时停止工作(当我调用片段时)

我在 Activity 中使用的代码(工作正常的代码)

 ImageButton btn = (ImageButton)findViewById(R.id.drawerButton);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
                drawer.openDrawer(Gravity.START);
            }
        });
Run Code Online (Sandbox Code Playgroud)

fragment_achievements.xml

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="layout.AchievementsFragment">

    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="28dp"
        android:layout_height="29dp"
        android:layout_marginLeft="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:background="@android:color/transparent"
        android:scaleType="fitCenter"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_menu_button" />

    <WebView
        android:id="@+id/web_ach"
        android:layout_width="409dp"
        android:layout_height="602dp"
        android:layout_marginBottom="-9dp"
        android:layout_marginLeft="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        tools:layout_editor_absoluteY="56dp" />

</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

成就片段.java

package layout;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.DrawerLayout;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView; …
Run Code Online (Sandbox Code Playgroud)

android android-fragments android-studio android-drawer

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

在lambda内打印

我目前正在使用这样的语法:

print(*list(map(lambda a: [something to do], input())))
Run Code Online (Sandbox Code Playgroud)

如果返回类型是字符串但不是数字,它工作正常.

例如.以下脚本用于在两个不同版本中查找三个的平方根

print(*list(map(lambda a: int(a)**(1/3), input())))
Run Code Online (Sandbox Code Playgroud)

SQRT = lambda a: a**(1/3)
print(SQRT(int(input())))
Run Code Online (Sandbox Code Playgroud)

当我输入9两个返回2.080083823051904(这是正确的)但是当我输入10第一个返回1.0 0.02.154434690031884第二个返回时.

我想知道有什么方法可以直接从lambda打印,返回一个数字而不会导致上面解决的问题?

python python-3.x

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