Javascript 使用邮递员发布数据后未获取数据
这是我的nodejs代码,我在其中一步一步地注释了我对代码所做的操作,现在我将使用POSTMAN发布数据Javascript 使用邮递员发布数据后未获取数据,javascript,node.js,mongodb,express,mongoose,Javascript,Node.js,Mongodb,Express,Mongoose,这是我的nodejs代码,我在其中一步一步地注释了我对代码所做的操作,现在我将使用POSTMAN发布数据 exports.votes = async (req, res, next) => { try { /** * 1. get the poll from db * 2. check if the user already exists in any option * 3. if user has already selected any
exports.votes = async (req, res, next) => {
try {
/**
* 1. get the poll from db
* 2. check if the user already exists in any option
* 3. if user has already selected any option do nothing
* 4. if user has selected any other option remove from that option
* 5. if user does not exist in any option, insert his user id to selected option
*/
const { pollId } = req.params;
console.log(pollId);
let { userId, answer } = req.body;
console.log(userId);
console.log(answer);
// get selected poll from db
const poll = await Poll.findByIdAndUpdate(pollId, {
$set: { options: poll.options },
}, {new: true});
if (answer && poll) {
answer = answer.toLowerCase();
///Finf the Poll
let existingVote = null;
Object.keys(poll.options).forEach((option) => {
// loop on all options, check if the user already exists in any option
if (poll.options[option].indexOf(userId) !== -1) {
existingVote = option;
}
});
if (existingVote == null) {
// if there is no existing vote save it to db
try {
const push = {};
push[`options.${answer}`] = userId;
const update = await Poll.findByIdAndUpdate(
pollId,
{ $push: push },
{ upsert: true }
);
res.status(201).json(update);
} catch (err) {
error.status = 400;
next(error);
}
} else if (existingVote && existingVote.length > 0) {
// check if answer is same as previous, if yes send not modified
if (existingVote.toLowerCase() === answer.toLowerCase()) {
res.status(304).send("Response already saved");
} else {
// delete the previous response and save it in new
if (
Array.isArray(poll.options[existingVote]) &&
poll.options[existingVote].length > 0
) {
// TODO: filtering this is not returning array but 1
poll.options[existingVote] = poll.options[existingVote].filter(
(vote) => vote != userId
);
poll.options[answer] = poll.options[answer].push(userId);
const update = await Poll.findByIdAndUpdate(pollId, {
$set: { options: poll.options },
});
res.status(201).json(update);
}
}
} else {
error = {
status: 500,
message: "Something went wrong",
};
next(error);
}
} else {
error = {
status: 404,
message: "Poll not found",
};
next(error);
}
} catch (error) {
error.status = 400;
next(error);
}
};
但每当我通过邮递员发布数据时,什么也得不到-
在这里,您可以看到我将数据发布到我的mongodb,但在这里什么也没有得到,我发布了类似{userId,answer}的数据,但得到了空白数组。。!
但当我检查我的ROBO-3T数据库时,我得到的数据->
我的问题是,为什么在我的数据库中获取数据而不是邮递员结果时会发生这种情况?
请帮忙
我也收到了这个警告-
(节点:8776)不推荐使用警告:Mongoose:findOneAndUpdate()
和findOneAndDelete()
未将useFindAndModify
选项设置为false的情况下不推荐使用。请参阅:来自:
默认情况下,findByIdAndUpdate()返回与以前相同的文档
已应用更新。如果设置new:true,则findOneAndUpdate()将
而是在应用更新后为您提供对象
因此,您可以尝试在查询选项中设置new
选项:
const update = await Poll.findByIdAndUpdate(pollId, {
$set: { options: poll.options },
}, {new: true});
您的代码中还有另一个
findbyiandupdate
,是否也传递了新选项?