我在将数据推送到mongodb中的收集时遇到了问题
const mongoose=require('mongoose');
const jwt=require('jsonwebtoken');
const _=require('lodash');
const express=require('express');
const bodyParser=require('body-parser');
mongoose.Promise=global.Promise;
mongoose.connect('mongodb://localhost:27017/mydata');
var app=express();
app.use(bodyParser.json());
var UserSchema=new mongoose.Schema({
email:{
type:String,
required:true
},
tokens:[{
access:{
type:String,
require:true
},
token:{
type:String,
required:true
}
}]
});
UserSchema.methods.generateAuthToken=function(){
var user=this;
var access='auth';
var token=jwt.sign({_id:user._id.toHexString(),access},'123abc').toString();
user.tokens.push({access,token});
return user.save().then((token)=>{
return token;
});
};
var User=mongoose.model('users',UserSchema);
app.post('/testapp',(req,res)=>{
var body=_.pick(req.body,['email']);
var user=new User(body);
user.save().then(()=>{
return user.generateAuthToken();
}).then((token)=>{
res.send(user);
}).catch((e)=>{
res.status(400).send(e);
});
});
app.listen(3000,()=>{
console.log(`server is
//----------
port:${3000}`);
});
结果电子邮件已保存,但无法将数据推送到令牌中并显示 邮递员中的这种错误。
{
"name": "MongoError",
"message": "Unknown modifier: $pushAll",
"driver": true,
"index": 0,
"code": 9,
"errmsg": "Unknown modifier: $pushAll"
}