Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js findOne返回一个空对象_Node.js_Mongodb_Express_Mongoose_Orm - Fatal编程技术网

Node.js findOne返回一个空对象

Node.js findOne返回一个空对象,node.js,mongodb,express,mongoose,orm,Node.js,Mongodb,Express,Mongoose,Orm,我试图通过\u id查找和删除子文档,但它总是返回null 这是我的用户模式: // Create sub-document schema for items var itemSchema = new mongoose.Schema ({ type: String, colour: String, material: String, brand: String, _id: String, }) // Main user schema (incl. wardrobe)

我试图通过
\u id
查找和删除子文档,但它总是返回null

这是我的
用户
模式:

// Create sub-document schema for items

var itemSchema = new mongoose.Schema ({
  type: String, 
  colour: String, 
  material: String,
  brand: String,
  _id: String,
})

// Main user schema (incl. wardrobe)

const userSchema = new mongoose.Schema({
  email: { type: String, unique: true },
  password: String,
  passwordResetToken: String,
  passwordResetExpires: Date,

  facebook: String,
  twitter: String,
  google: String,
  github: String,
  instagram: String,
  linkedin: String,
  steam: String,
  tokens: Array,

  profile: {
    name: String,
    gender: String,
    location: String,
    website: String,
    picture: String
  },

  wardrobe: {
    items: [[itemSchema]]
  }

}, { timestamps: true });
这是一个示例对象:

{
    "_id": {
        "$oid": "5c16d181c15a68c14fe2f2d3"
    },
    "wardrobe": {
        "items": [
            [
                {
                    "type": "T-shirt",
                    "colour": "Black",
                    "material": "Wool",
                    "brand": "Adidas",
                    "_id": "5c1827dcc13025f86gfdf053"
                }
            ],
            [
                {
                    "type": "T-shirt",
                    "colour": "Black",
                    "material": "Cotton",
                    "brand": "Puma",
                    "_id": "5c1827dfc13025f86dfaf075"
                }
            ]
        ]
    },
    "tokens": [],
    "email": "test@test.com",
    "password": "$2a$10$tyCl4frHgAA1m8p6Y6kAkeAbjdYK1SJ5ghnlvWCF/MAb9.hMfR2/a",
    "createdAt": {
        "$date": "2018-12-16T22:28:17.777Z"
    },
    "updatedAt": {
        "$date": "2018-12-17T23:10:02.301Z"
    },
    "__v": 16,
    "profile": {
        "gender": "",
        "location": "",
        "name": "Test",
        "website": ""
    }
}
这就是我使用findOne的方式:

exports.deleteItem = (req, res, next) => {
    User.findOne({"wardrobe.items._id": "5c1827dfc13025f86dfaf075"}, (err, item) => {
    console.log(item);
    if (err) {
        return console.log("error: " + err);
      }
      res.redirect('/wardrobe');      
    });
  };
console.log(item)
始终返回null,即使id字段是现有文档。有什么想法吗

exports.deleteItem = (req, res, next) => {
    User.findOne({"wardrobe.items[0]._id": "5c1827dfc13025f86dfaf075"}, (err, item) => {
    console.log(item);
    if (err) {
        return console.log("error: " + err);
      }
      res.redirect('/wardrobe');      
    });
  };

衣柜
是一个对象,但
物品
是一个数组,因此您应该先购买物品

经过多次尝试和错误后,我需要iTunes推出
$elemMatch
。这将返回整个对象:

exports.deleteItem = (req, res, next) => {
    User.findOne({ 'wardrobe.items': { $elemMatch: { "_id": "5c1985359db5074cb3da562d",} } }, {wardrobe:1}, (err, item) => {
    console.log(item);
    if (err) {
        return console.log("error: " + err);
      }
      res.redirect('/wardrobe');      
    });
  };

但是,我不想要整个对象,我想要搜索具有
\u id
的特定对象。但这是另一个问题。

您可以在mongo中添加文档以获取更多信息吗?添加了一个示例谢谢,但这不起作用
console.log(item)
仍然返回空值。
{corpore.items[0]。\u id:“5c1827dfc13025f86dfaf075”}
它是最后一个。你能试试吗?运气不好。这样,我得到:
SyntaxError:Unexpected token.
在示例文档中,子文档嵌套在两个数组中。这应该是原因,请尝试使用
{camport.items[0][0]。\u id}