使用 postman API 尝试 post 数据时出现错误“非法参数:字符串,未定义”
Having an error " Illegal arguments: string, undefined " while trying to post data using postman API
对于使用 bcrypt 的节点中的用户身份验证,我有以下代码,但是当我尝试使用 postman api post 时出现错误,错误是 "message": "Illegal arguments: string, undefined"
通过postman发送的数据是
{
"password":"1234",
"email":"prashanthyarram@gmail.com"
}
我找不到代码有什么问题,你能帮我解决这个问题吗?
这是 server.js
的代码
import express from 'express';
import userdata from './userdata.js';
import mongoose from 'mongoose';
import userrouter from './routers/userrouter.js';
import productRouter from './routers/productrouter.js';
const app=express();
mongoose.connect(process.env.MONGODB_URL || 'mongodb://localhost:27017/purpleshades',{
useUnifiedTopology: true,
useNewUrlParser: true
})
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.get('/users/api/:id',(req,res)=>{
const k=parseInt(req.params.id)
const user1=userdata.users.find((x)=>x.uID===k)
const cartinfo=user1.ucart
const cartproductlist=[]
for (let i = 0; i < cartinfo.length; i++) {
const pid = cartinfo[i];
const productreal = data.products.find((x) => x.id === pid );
cartproductlist.push(productreal)
}
res.send(cartproductlist)
})
app.use('/api/users',userrouter)
app.use('/api/products',productRouter)
app.use((err,req,res,next)=>{
res.status(500).send({message:err.message})
})
const port=process.env.PORT || 5000
app.listen(5000,()=>{
console.log(`serve at http://localhost:${port}`);
})
代码 userrouter.js
import express from "express";
import userdata from "../userdata.js";
import User from "../models/usermodel.js";
import expressAsyncHandler from "express-async-handler";
import bcrypt from 'bcryptjs'
const userrouter=express.Router()
userrouter.get('/seed',expressAsyncHandler( async(req,res)=>{
const createdusers=await User.insertMany(userdata.users)
res.send({createdusers})
}))
userrouter.post('/signin',expressAsyncHandler(async(req,res)=>{
const user=User.findOne({email: req.body.email})
if(user){
if(bcrypt.compareSync(req.body.password,user.password)){
res.send({
_id:user._id,
name:user.name,
isAdmin:user.isAdmin,
})
return
}
}
res.status(401).send({message:'invalid Email or Password'})
}))
export default userrouter
usermodel.js
代码
import mongoose from "mongoose";
const userschema=new mongoose.Schema(
{
name:{type:String,required:true},
email:{type:String,required:true,unique:true},
password:{type:String,required:true},
isadmin:{type: Boolean,required:true ,default:false}
},
{
timestamps:true
}
)
const User=mongoose.model('User',userschema)
export default User
findOne
函数是异步的,所以这一行给变量赋了一个promiseuser
:
const user=User.findOne({email: req.body.email})
由于 promise 是真实的,这会调用 compareSync
,但 promise 没有 password
属性:
if(user){
if(bcrypt.compareSync(req.body.password,user.password)){
等待 findOne 的结果来获取文档而不是承诺。
对于使用 bcrypt 的节点中的用户身份验证,我有以下代码,但是当我尝试使用 postman api post 时出现错误,错误是 "message": "Illegal arguments: string, undefined"
通过postman发送的数据是
{
"password":"1234",
"email":"prashanthyarram@gmail.com"
}
我找不到代码有什么问题,你能帮我解决这个问题吗?
这是 server.js
的代码import express from 'express';
import userdata from './userdata.js';
import mongoose from 'mongoose';
import userrouter from './routers/userrouter.js';
import productRouter from './routers/productrouter.js';
const app=express();
mongoose.connect(process.env.MONGODB_URL || 'mongodb://localhost:27017/purpleshades',{
useUnifiedTopology: true,
useNewUrlParser: true
})
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.get('/users/api/:id',(req,res)=>{
const k=parseInt(req.params.id)
const user1=userdata.users.find((x)=>x.uID===k)
const cartinfo=user1.ucart
const cartproductlist=[]
for (let i = 0; i < cartinfo.length; i++) {
const pid = cartinfo[i];
const productreal = data.products.find((x) => x.id === pid );
cartproductlist.push(productreal)
}
res.send(cartproductlist)
})
app.use('/api/users',userrouter)
app.use('/api/products',productRouter)
app.use((err,req,res,next)=>{
res.status(500).send({message:err.message})
})
const port=process.env.PORT || 5000
app.listen(5000,()=>{
console.log(`serve at http://localhost:${port}`);
})
代码 userrouter.js
import express from "express";
import userdata from "../userdata.js";
import User from "../models/usermodel.js";
import expressAsyncHandler from "express-async-handler";
import bcrypt from 'bcryptjs'
const userrouter=express.Router()
userrouter.get('/seed',expressAsyncHandler( async(req,res)=>{
const createdusers=await User.insertMany(userdata.users)
res.send({createdusers})
}))
userrouter.post('/signin',expressAsyncHandler(async(req,res)=>{
const user=User.findOne({email: req.body.email})
if(user){
if(bcrypt.compareSync(req.body.password,user.password)){
res.send({
_id:user._id,
name:user.name,
isAdmin:user.isAdmin,
})
return
}
}
res.status(401).send({message:'invalid Email or Password'})
}))
export default userrouter
usermodel.js
代码import mongoose from "mongoose";
const userschema=new mongoose.Schema(
{
name:{type:String,required:true},
email:{type:String,required:true,unique:true},
password:{type:String,required:true},
isadmin:{type: Boolean,required:true ,default:false}
},
{
timestamps:true
}
)
const User=mongoose.model('User',userschema)
export default User
findOne
函数是异步的,所以这一行给变量赋了一个promiseuser
:
const user=User.findOne({email: req.body.email})
由于 promise 是真实的,这会调用 compareSync
,但 promise 没有 password
属性:
if(user){
if(bcrypt.compareSync(req.body.password,user.password)){
等待 findOne 的结果来获取文档而不是承诺。