【发布时间】:2018-10-16 21:31:24
【问题描述】:
我正在尝试使用请求模块向 API 发送 POST 请求,但是当我 console.log 发送的数据时,似乎只发送了time、approved 状态和id,这是默认情况下发送的来自模型模式,但不发送其余数据。我需要帮助。
这是发送的
而不是这样的
console.log(req.body) 当我使用 Postman 发送数据时
let mongoose = require('mongoose');
//models config
let jobSchema = new mongoose.Schema({
title: String,
category: String,
description: String,
type: String,
url: String,
email: String,
apply: String,
location: String,
company: String,
// path: String,
approved: {type: Boolean, default: false},
created: {type: Date, default: Date.now, expires: 1000}
})
let Job = mongoose.model('Job', jobSchema);
module.exports = Job;
//This is from another file :helpers
exports.createJob = (req, res) => {
db.Job.create(req.body)
.then((newJob) => {
res.status(201).json(newJob)
})
.catch((err) => {
res.send(err)
})
}
//post
app.get('/jobs/add', (req, res) => {
res.render('add')
})
app.post('/jobs', (req, res)=>{
// let formBody = {
// title: req.title,
// category: req.category,
// description: req.body.description,
// type: req.body.type,
// url: req.body.url,
// email: req.bodyemail,
// apply: req.body.apply,
// location: req.body.location,
// company: req.body.company,
// // path: fullPath,
// createdAt: Date.now()
// };
console.log()
request.post({url:'http://localhost:3000/api/jobs/', form: {key:'value'}}, function optionalCallback(err, response, body) {
if (err) {
return console.error('upload failed:', err);
}else{
console.log('Upload successful! Server responded with:', body);
}
return res.redirect('/jobs')
});
})
<div class="add-container">
<form name="myForm" action="/jobs" method="POST" >
<div class="form-group">
<h2>Title(Junior/Graduate/Intern)</h2>
<input type="text" name="job[title]" placeholder="e.g: Junior Front-End Developer" class="form-control" id="title" required="title">
</div>
<div class="form-group">
<h2>Company Name</h2>
<input type="text" name="company" placeholder="e.g: Microsoft" class="form-control" required="company">
</div>
<div class="form-group">
<h2>Job Description</h2>
<textarea id="mytextarea" name="description" placeholder="e.g: We are looking for a Front-End developer with about a year of experience in HTML, CSS, javaScript. Knowledge in React/Vue/Angular is a plus." class="form-control"></textarea>
</div>
<div class="form-group">
<h2>Apply by Website</h2>
<input type="text" name="url" placeholder="e.g: https://wwww.example.com/jobs" class="form-control">
</div>
<div class="form-group">
<h2>How to Apply</h2>
<input type="text" name="apply" placeholder="e.g: send your CV or Resume to..." class="form-control" required="apply">
</div>
<div class="form-group">
<h2>Company Location</h2>
<input type="text" name="location" placeholder="e.g Lagos" class="form-control" required="location">
</div>
<div class="form-group">
<label for="email">Apply by Email</label>
<input type="email" name="email" class="form-control" id="email" placeholder="e.g: job@gmail.com" required="email">
</div>
<!-- <div class="form-group">
<label>Company logo</label>
<input type="file" name="file" single class="form-control" id="file" required="file">
</div> -->
<button type="submit" class="btn btn-default">Submit</button>
</form>
</div>
【问题讨论】:
-
您将表单正文发布为什么? JSON? url编码?
-
看起来你正在尝试做formdata
-
我将我的正文发布为 url 编码。是的,我想做表单数据,但现在我只想能够发布数据
标签: javascript node.js post request