Node.js 无法在平均堆栈中使用findOneAndUpdate更新请求
我想使用名为Node.js 无法在平均堆栈中使用findOneAndUpdate更新请求,node.js,mongodb,express,mongoose,mean-stack,Node.js,Mongodb,Express,Mongoose,Mean Stack,我想使用名为token的字段在中发布请求。这是我的猫鼬模式 const UserSchema = mongoose.Schema({ name: { type: String }, email: { type: String, required: true, unique: true }, username: { type: String, required: true, unique: true }, pass
token
的字段在中发布请求。这是我的猫鼬模式
const UserSchema = mongoose.Schema({
name: {
type: String
},
email: {
type: String,
required: true,
unique: true
},
username: {
type: String,
required: true,
unique: true
},
password: {
type: String,
required: true
},
phone: {
type: String
},
resetPasswordToken: {
type: String
},
resetPasswordExpires: {
type: Date
}
});
这是我从user.js
router.put('/reset/:token', function(req, res) {
console.log('listening');
User.findOneAndUpdate(
{resetPasswordToken:req.params.token},
{
password: req.body.password,
resetPasswordToken: undefined,
resetPasswordExpires: undefined
},
function(err,user) {
if(err) {
console.log(err + 'is here');
} else {
res.json(user);
}
}
);
});
这是我发邮件请求的地方
resetPassword(passwordData) {
let headers = new Headers();
console.log(passwordData);
headers.append('Content-Type','application/json');
return this.http.put('http://localhost:3000/api/reset/'+passwordData.token,passwordData,{headers: headers})
.map(res => res.json());
}
没有任何操作发生,我无法在cmd中看到正在侦听的。对于用户,我已将令牌值设置为
"resetPasswordToken" : "2a287acacf7d5e98de9a158c8be82744ef0302f9"
我可以使用邮递员进行更新,但无法使用代码进行更新。如果您能够通过邮递员而不是客户端处理请求,则有两种可能性:
- CORS:在处理所有请求之前,请使用以下代码
app.use((请求、恢复、下一步)=>{
res.header(“访问控制允许方法”、“获取、放置、发布、删除”);
下一个()
})
- 使用x-http-method-override标头:有时不允许As put或delete请求。
这可以在处理请求之前在客户端或服务器中发送时完成()
是的,我使用的是app.use(函数(req,res,next){res.header(“访问控制允许来源”,“*”);res.header(“访问控制允许方法”,“获取,来源,选项,发布,放置”);res.header(“访问控制允许来源”,“X请求来源,内容类型,接受,授权”);next();})代码>但我仍然面临着一个问题,我也尝试过不改变代码。请在代码中使用parenthasis。使用方法覆盖标题var methodOverride=require('method-override')代码>app.use(methodOverride())代码>是的,我正在使用它们……在设置了Put的覆盖之后,我该怎么做?你能描述一下吗?