简体   繁体   English

NodeJS req.body 在函数 nodejs 中为空 - Rest API 带有 ExpressJs 和续集

[英]NodeJS req.body empty in funtion nodejs - Rest API whit ExpressJs and sequelize

I'm using ExpressJS and sequelize.我正在使用 ExpressJS 和续集。 I need invoke two functions ("accoun.js" and "people.js") into a main function (fullaccount.js) receving data in "fullaccount.js" for submit and process in the others functions, I received ready req.body in "fullaccount.js" but in "accoun.js" and "people.js" REQ.BODY IS EMPTY我需要将两个函数(“accoun.js”和“people.js”)调用到一个主 function(fullaccount.js)中,在“fullaccount.js”中接收数据以在其他函数中提交和处理,我收到了准备好的 req.body在“fullaccount.js”中,但在“accoun.js”和“people.js”中,REQ.BODY 为空

app.js应用程序.js

const express = require('express');
const morgan = require('morgan');

var app= express(); 
app.set('port',process.env.PORT || 4094); 
app.use(morgan('dev')); 
app.use(express.urlencoded({extended:false})); 
app.use(express.json());        
app.use(require('./routes/back/account.route'));
app.use(require('./routes/back/people.route'));
app.listen(app.get('port'), function(){
  console.log('Working in port: '+app.get('port'));
});

fullaccount.js fullaccount.js

const account=require( './account.ctrl');
const people= require ('./people.ctrl');
import Sequelize from 'sequelize';

export function add(req, res) {
  console.log(req.body); //-> it's ready. RETURNING JSON
  const {nick,pass,email,firstNam,lastName,identCard,birthDate,genderId,countryId}=req.body;

  account.add({nick,pass,email},res); //=>> invoke add method from account controller return undefined
  people.add({firstName,lastName,identCard,birthDate,genderId,countryId},res); //=>> invoke add method from people controller return undefined
}

account.js account.js

import Account from '../../db/models/account.mdl';

export function add(req, res) {
  console.log(req.body); // it's return undefined 
};

people.js人.js

import People from '../../db/models/people.mdl'; 

export function add(req, res) {
  console.log(req.body); // it's return undefined  
};

I don't know what the error is, I need help我不知道错误是什么,我需要帮助

req.body is undefined because you are not passing the initial request to account.add or people.add req.body undefined ,因为您没有将初始request传递给account.addpeople.add

For example, for account, you are actually passing an object that contains nick , pass and email例如,对于帐户,您实际上传递的是包含nickpass和 email 的email

account.add({nick, pass, email}, res); // req.nick, req.pass, req.email are available

To avoid confusion, you should rename the req parameter in account.add to data .为避免混淆,您应该将account.add中的req参数重命名为data Good names will save you a lot of headaches.好名字会让你省去很多麻烦。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM