这是调用请求的注册路线
User.register(new User({username: req.body.username}),
req.body.password, (err, user) => {
if(err) {
res.statusCode = 500;
res.setHeader('Content-Type', 'application/json');
res.json({err: err});
}
else {
if(req.body.admin) {
user.admin = req.body.admin;
}
if(req.body.hostel) {
user.hostel = req.body.hostel;
}
user.save((err, user) => {
if(err) {
res.statusCode = 500;
res.setHeader('Content-Type', 'application/json');
res.json({err: err});
return;
}
passport.authenticate('local')(req, res, () => {
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.json({success: true, status: 'Registration Successful!'});
});
});
}
});
});
此代码段来自调用请求的位置。 我在for循环中调用了axios帖子,以从一个CSV文件中注册多个用户
const csv = require('csv-parser')
const fs = require('fs')
var axios = require('axios');
const results = [];
fs.createReadStream('data.csv')
.pipe(csv())
.on('data', (data) => results.push(data))
.on('end', () => {
//console.log(results);
for(var i = 10; i <= 20; i++) {
axios.post('http://localhost:3000/users/signup',
{
password: results[i].DOB,
username: results[i].Sid,
hostel: '5f37923011855135682d1ae8'
})
.then((res) => {
console.log(`Status: ${res.status}`);
console.log('Body: ', res.data);
}).catch((err) => {
console.error(err);
});
}
});
发布单个数据返回成功时,否则服务器生成错误代码500