Return null default value if no result found

后端 未结 2 1028
野性不改
野性不改 2020-12-19 17:14

I have a collection that looks like this:

{  
\"value\" : \"20\",
\"type\" : \"square\",
\"name\" : \"form1\"
}
{
\"value\" : \"24\",
\"type\" : \"circle\",
         


        
2条回答
  •  隐瞒了意图╮
    2020-12-19 17:52

    Why you dont check in callback if result==null and create your own empty object?

    let name = "form4";
    db.myCollec.find({"name":name} , {"name":1, "type":1, "_id":0}, function(err, result){
        if(err) {
             // Error handling
             return;
        }
        if (result==null){
            result = {"name":name, "type":null};
        }
    });
    

提交回复
热议问题