简体   繁体   English

无法将 { } 添加到 nodejs 中的 object 到 mongodb

[英]Can't add { } to object in nodejs to mongodb

I am trying to insert a document into a collection only if the parent's document exists, but I can't escape the {} from the object.仅当父文档存在时,我才尝试将文档插入集合,但我无法从 object 中转义 {}。

app.post("/addSkill", function(request, response){
    
    collection = db.collection("skills");

    var skill_category = request.body.skill_category;
    var skill_subcategory = request.body.skill_subcategory;
    var skill_name = request.body.skill_name;

    var firstData = {
        \{ \}, \{ skill_category \}
    };
    console.log("First data: " + firstData);
    var secondData = {
        \{ \}, \{ skill_category: \{ subcategory \}\}
    };
    console.log("Second data: " + secondData);


    if(collection.find(firstData)){
        console.log("Join to first data");
        if(collection.find(secondData)){
            console.log("Join to second data");
        }
        else{
            console.log("Not join to second data");
        }
    }
    else{
        console.log("Not join to first data");
    }
    return response.redirect('/');
});

The above code gives this error:上面的代码给出了这个错误:

\{ \}, \{ skill_category \}
^
SyntaxError: Invalid or unexpected token
var firstData = {
    `{ }, { ${skill_category} }`
};

The above code gives this error:上面的代码给出了这个错误:

`{ }, { ${skill_category} }`
^^^^^^^^^^
SyntaxError: Unexpected template string
if(collection.find({category:{$exists:true}})){}
if(collection.find({"category.subcategory":{$exists:true}})){}

Exists 存在

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM