向mongoose中的映射添加异步问题



我正试图将我的googleID(对象(添加到另一个猫鼬模式中的映射中。我字符串化了我的用户id对象,在我的测试代码中,我遇到了这个错误:

(node:293528) UnhandledPromiseRejectionWarning: ValidationError: collection validation failed: likes.5dbf6205bdcd5e5f78536447: Cast to ObjectId failed for value "110840542851551561690" at path "likes.$*"
at new ValidationError (C:filesnode_modulesmongooseliberrorvalidation.js:30:11)
at model.Document.invalidate (C:filesnode_modulesmongooselibdocument.js:2333:32)
at Map.set (C:filesnode_modulesmongooselibtypesmap.js:71:26)
at C:filesapp.js:123:30
at C:filesnode_modulesmongooselibmodel.js:4589:16
at C:filesnode_modulesmongooselibquery.js:4323:12
at process.nextTick (C:filesnode_modulesmongooselibquery.js:2805:28)
at process._tickCallback (internal/process/next_tick.js:61:11)
(node:293528) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:293528) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

来自client.js的我的帖子请求:

const likesForm = document.getElementById("test");
likesForm.addEventListener("submit", function (evt) {
evt.preventDefault();
console.log(event.target.id)
const [likeString, itemId] = evt.target.id.split("-");
vote(
likeString === "like"
? "1"
: "-1",
itemId
);
}, false);
async function vote (voteInc, itemId) {
const route = `/like/${itemId}`;
const response = await fetch(route, {
method: 'POST',
body: JSON.stringify({"like": voteInc}),
headers: {
'Content-Type': 'application/json'
}
});
const result = await response.json();
console.log(result);
}

服务器端帖子:

app.post("/like/:id",(req,res)=>{
const id = req.params.id;
const like = req.body.like;
console.log(like);
console.log(id);
if(req.isAuthenticated()){
linkscr.user.findById(req.user.id, (e,foundUser)=>{
if(e){
console.log(e);
}else{
//if like = 1
if(like==1){
linkscr.collection.findById(id, function(error,result){
if(error){
console.log(error);
}else{
result.likes.set(foundUser._id.toString(),foundUser.googleId);
result.save();
}
})
console.log("not voted yet. Add 1 and added to like");
}else{
linkscr.collection.findById(id, function(error,result){
if(error){
console.log(error);
}else{
result.dislikes.set(foundUser._id.toString(),foundUser.googleId);
result.save();
}
})
console.log("not voted yet. Add 1 and added to dislike");   
};
};
});
}else{
res.redirect("/");
}
});

我的好恶模式:

likes        : { 
type: Map, 
of: { 
type: mongoose.Schema.Types.ObjectId,
ref: "User" }
},
dislikes     : { 
type: Map, 
of: { 
type: mongoose.Schema.Types.ObjectId,
ref: "User" }
},

架构最初是如何保存的:

likes        : {[User._id]:User},
dislikes     : {},

谢谢你的帮助。你是我唯一的希望Stack!

我最初认为这是一个关于我如何处理对象的猫鼬问题,但看起来这更多地与异步有关。无可否认,这是我写这篇文章时在编程方面最大的弱点。

编辑:根据我从文档中看到的内容,猫鼬不能只设置对象字符串。Mongo不会猫鼬。

编辑2:我的坏东西放错了Id。foundUser._id.toString() ,foundUser

要在mongoose中创建映射,键**必须是字符串形式。此外,您的模式(最初没有共享(想要的是object_id,而不是google passportID。为了帮助子孙后代,我将在原始帖子中进行编辑。在这种情况下,使用foundUser进行简化即可。

result.likes.set(foundUser._id.toString(), foundUser);

最新更新