简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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