将数据传递给服务器显示未定义
Posted
技术标签:
【中文标题】将数据传递给服务器显示未定义【英文标题】:Passing data to server showing undefined 【发布时间】:2020-10-16 19:36:03 【问题描述】:我是 React JS 和 Flux 的新手,我想将我的数据从我的 React 表单传递到我的服务器,在服务器中我有一个发送电子邮件的代码,我使用的是 Flux 架构。当我运行我的代码时,它可以正常工作,只有数据未定义。谁能帮我?这是我的代码
这是我的 Store.js
import EventEmitter from 'events';
import Dispatcher from '../dispatcher/Dispatcher';
import MessageConstants from '../constants/MessageConstants';
class MessageStore extends EventEmitter
constructor()
super()
this.supportTicket = [
fullName: '',
site: '',
email: '',
subject: '',
description: '',
type: ''
];
sendMessage(fullName, site, email, subject, description, type)
this.supportTicket.push(
fullName,
site,
email,
subject,
description,
type
);
this.emit("change");
const MessageStore = new MessageStore;
Dispatcher.register(function(action)
switch (action.actionType)
case MessageConstants.PERFORM_SEND_MESSAGE:
this.sendMessage(action.fullName, action.site, action.email, action.subject, action.description)
break;
default:
// no op
);
export default MessageStore;
这是 action.js
import _ from 'lodash';
import jQuery from 'jquery';
import Dispatcher from '../dispatcher/Dispatcher';
import MessageConstants from '../constants/MessageConstants';
function sendMessage(fullName, site, email, subject, description, type, callback)
jQuery.ajax(
method: 'POST',
url: '/api/v1/supportTicket',
contentType: 'aplication/json',
data: JSON.stringify(
fullName: fullName,
site: site,
email: email,
subject: subject,
description: description,
type
)
)
.done(function(sendMessage)
callback(null, sendMessage);
)
.fail(function(err)
console.error('Failed to send message', JSON.stringify(err));
callback(err);
);
const MessageActions =
sendMessage: function(fullName, site, email, subject, description, callback)
sendMessage(fullName, site, email, subject, description, function(err, fullName, site, email, subject, description, type)
if(err) return callback(err);
Dispatcher.dispatch(
actionType: MessageConstants.PERFORM_SEND_MESSAGE,
fullName: fullName,
site: site,
email: email,
subject: subject,
description: description,
type: type
);
callback(null, fullName)
);
;
module.exports = MessageActions;
这是我处理提交数据的方法
handleSubmit(evt)
evt.preventDefault();
this.handleProfileChange();
// this.handleSubjectChange(evt);
// this.handleMessageChange(evt);
// var messageInfo =
// subject: this.state.subject,
// description: this.state.description
// ;
console.log(this.state.fullName);
console.log(this.state.site);
console.log(this.state.email);
console.log(this.state.subject);
console.log(this.state.description);
this.setState( submitting: true , () =>
MessageActions.sendMessage(this.state.fullName, this.state.site, this.state.email, this.state.subject, this.state.description, (err) =>
if(err)
this.setState(
submitting: false,
errorMessage: errorText(err)
);
);
);
这是给我的后端
const express = require('express');
const router = express.Router();
const email = require('nodemailer');
const routing = requirerp('/lib/routing');
const Err = requirerp('/utils/restErrors');
//let account = await email.createTestAccount();
let transporter = email.createTransport(
service: "Gmail",
host: "smtp.gmail.com",
auth:
user: 'xxxx@gmail.com',
pass: 'xxxxx'
);
router.post('/', routing.requireAuth, function(req, res, next)
var fullName = req.body.fullName
var site = req.body.site
var email = req.body.email
var subject = req.body.subject
var description = req.body.description
var type = req.body.type
var content = 'Nama : ' + fullName + '\nLokasi : ' + site + '\nPesan : ' + description
let mailOptions =
from: fullName + '<' + email + '>',
to: 'xxxxx@gmail.com',
subject: 'Request :' + subject,
text: content
transporter.sendMail(mailOptions, function(err, info)
if(Err.invalidInput(next, err))
return;
console.log('Error' + info);
else
res.json(
status: 'success'
)
console.log('Email has been sent' + info.response);
)
)
module.exports = router;
【问题讨论】:
【参考方案1】:在后端尝试这样的事情:
const myObj = req.body.data
var fullName = myObj.fullName
var site = myObj.site
...
【讨论】:
它没有做任何改变,数据仍然显示未定义。在我尝试再次运行我的代码之前我应该重新启动服务器吗? 好的,我已经更改了代码,并重新启动了服务器,但它仍然未定义,并且出现更多错误,有什么建议吗?以上是关于将数据传递给服务器显示未定义的主要内容,如果未能解决你的问题,请参考以下文章