質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
React Native

React Nativeは、ネイティブモバイルアプリ(iOS/Android)を作成できるJavaScriptフレームワークです。Reactと同じ設計のため、宣言的なコンポーネントでリッチなUIを開発することが可能です。

POST

POSTはHTTPプロトコルのリクエストメソッドです。ファイルをアップロードしたときや入力フォームが送信されたときなど、クライアントがデータをサーバに送る際に利用されます。

Express

ExpressはNode.jsのWebアプリケーションフレームワークです。 マルチページを構築するための機能セットおよびハイブリッドのWebアプリケーションを提供します。

Q&A

解決済

1回答

1443閲覧

React NativeにてaxiosのPOSTで値を送信できない

kazu235

総合スコア21

React Native

React Nativeは、ネイティブモバイルアプリ(iOS/Android)を作成できるJavaScriptフレームワークです。Reactと同じ設計のため、宣言的なコンポーネントでリッチなUIを開発することが可能です。

POST

POSTはHTTPプロトコルのリクエストメソッドです。ファイルをアップロードしたときや入力フォームが送信されたときなど、クライアントがデータをサーバに送る際に利用されます。

Express

ExpressはNode.jsのWebアプリケーションフレームワークです。 マルチページを構築するための機能セットおよびハイブリッドのWebアプリケーションを提供します。

0グッド

0クリップ

投稿2022/06/04 09:24

【環境】

使用技術
クライアントサイドReact Native、axios
サーバーサイドNode.js、Express

【バージョン】

ver
Node.jsv16.15.0
Express4.16.1

本題

React NativeにてaxiosのPOSTの挙動の確認をしているのですが、第二引数に指定したオブジェクトがbodyの中に入らずに困っています。

想定している結果は email:test@gmail.com です。

調べても出てくるのは送った値の整形のやり方ばかりで、送る段階の情報がなかなか見つからなかったです。

どなたかご回答をお願いいたします。

conosle.log(req)の結果

イメージ説明

app.js(React Native)

JavaScript

1import { StatusBar } from 'expo-status-bar'; 2import { StyleSheet, Text, View, Button, TextInput } from 'react-native'; 3import axios from 'axios'; 4// import { type } from 'express/lib/response'; 5 6const host = 'http://192.168.0.195:3000' 7 8export default function App() { 9 10 const sendTmpUser = async () => { 11 try { 12 await axios.post(host + '/api/tmp_user', 13 { email: 'test@gmail.com' } 14 ); 15 } catch (error) { 16 console.log(error); 17 18 } finally { 19 20 } 21 } 22 23 return ( 24 <View style={styles.container}> 25 <Text>メールアドレス</Text> 26 <TextInput 27 style={styles.input} 28 placeholder='mail' 29 /> 30 <Text>パスワード</Text> 31 <TextInput 32 style={styles.input} 33 placeholder='password' 34 /> 35 36 <Button 37 title='送信' 38 onPress={() => { 39 sendTmpUser(); 40 }} 41 /> 42 43 <StatusBar style="auto" /> 44 45 </View> 46 ); 47} 48 49const styles = StyleSheet.create({ 50 container: { 51 flex: 1, 52 backgroundColor: '#fff', 53 alignItems: 'center', 54 justifyContent: 'center', 55 }, 56 input: { 57 borderWidth: 1, 58 width: 150 59 }, 60}); 61

app.js(Node.js)

JavaScript

1const express = require('express'); 2const app = express(); 3const bodyParser = require('body-parser'); 4 5// app.use(express.json()); 6app.use(bodyParser.urlencoded({ extended: true })); 7 8app.use(express.urlencoded({ extended: false })); 9 10//routes 11const userRouter = require('./routes/user'); 12const tmpUserRouter = require('./routes/tmp_user'); 13const postRouter = require('./routes/post'); 14const handHistoryRouter = require('./routes/hand_history'); 15 16app.use('/api/user', userRouter); 17app.use('/api/tmp_user', tmpUserRouter); 18app.use('/api/post', postRouter); 19app.use('/api/hand_history', handHistoryRouter); 20 21app.listen(3000);

routes/tmp_user.js(Node.js)

JavaScript

1const express = require('express'); 2const app = express(); 3const router = express.Router(); 4const connection = require('../connetction') 5const uuid = require('node-uuid'); 6const bodyParser = require('body-parser'); 7 8// app.use(express.json()); 9app.use(bodyParser.urlencoded({ extended: false })); 10 11router.get('/', (req, res) => { 12}) 13 14router.post('/', (req, res) => { 15 console.log(req); 16}) 17 18module.exports = router;

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

以下の修正でどうでしょう?

diff

1- const bodyParser = require('body-parser');

diff

1- // app.use(express.json()); 2+ app.use(express.json()); 3- app.use(bodyParser.urlencoded({ extended: false })); 4+ app.use(express.urlencoded({ extended: false }));

投稿2022/06/04 12:05

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

kazu235

2022/06/04 12:59

ご回答ありがとうございます。 app.jsの方をご回答通り変更して、tmp_user.jsの方のbodyParserとexpress.json()を削除しましたら、想定した値を送ることができました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問