【问题标题】:UnhandledPromiseRejectionWarning: MongoError: The argument to $each in $push must be an array but it was of type: objectUnhandledPromiseRejectionWarning: MongoError: The argument to $each in $push must be an array but it is of type: object
【发布时间】:2019-03-07 04:45:11
【问题描述】:

我正在尝试使用嵌套数组向集合中的现有文档添加值。

我遇到的第一个问题是我无法将新域添加到我的域数组中

我不明白我做错了什么??

这是我想要在我的数据库中的示例:

{
"_id" : ObjectId("5c76a093aac6fa3f140a5672"),
"KdNr" : "10004", 
"Kundenname" : "Customer GmbH",
"__v" : 0,
 "domains" : [ {
  "domain" : "testB.de",
   "in" : [ {
    "content" : "New Graph B",
    "graph" : { ... } } ],
   "out" : [ {
    "content" : "Another new Graph B",
    "graph" : { ... } } ]
  }, [ {
  "domain" : "testA.de",
   "in" : [ {
    "content" : "New Graph A",
    "graph" : { ... } } ],
   "out" : [ {
    "content" : "Another new Graph A",
    "graph" : { ... } } ]
   } ]
 }

这是我得到的示例(不是我想要的):

{
"_id" : ObjectId("5c76a093aac6fa3f140a5672"),
"KdNr" : "10004", 
"Kundenname" : "Customer GmbH",
"__v" : 0,
 "domains" : [ {
   { "_id" : ObjectId("5c7f86ad42d63141fc921d04") },
   { "_id" : ObjectId("5c655c828be0b2b295aa126f") }
 ] }

这是我的架构:

const mongoose = require('mongoose');

const graphSchema = mongoose.Schema({
  graph: {
    nodes: [{
      id: { type: String, required: true, unique: true },
      label: { type: String, required: true },
      description: { type: String }
    }],
    links: [{
      source: { type: String, required: true },
      target: { type: String, required: true },
      label: { type: String }
    }]
  }
});

const domainSchema = mongoose.Schema({
  domain: {
    name: { type: String, unique: true, required: true },
    in: {
      content: { type: String },
      graphSchema
    },
    out: {
      content: { type: String },
      graphSchema
    }
  }
});

const diagramSchema = mongoose.Schema({
  KdNr: { type: String, required: true, index: true, unique: true },
  Kundenname: { type: String, required: true },
  domains: [{
    domainSchema
  }]
});

module.exports = mongoose.model('Diagram', diagramSchema);

这是我的 domain-routes.js:

// core Modules
const express = require('express');

// own modules
const Diagram = require('../models/diagrams');

const router = express.Router();

// add Domain to Diagram
router.put('/:KdNr', (req, res, next) => {

  console.log(req.body.domain);
  const data = {
      domain: req.body.domain,
      in: [{
        content : "New Graph",
        graph : {}
      }],
      out: [{
        content : "New Graph",
        graph : {}
      }]
  }

  Diagram.updateOne(
    { KdNr: req.params.KdNr },
    { $push: { domains: { $each: data } } } // Seems like here is some problem?
    // { $push: data }
  ).then(result => {
    if(result) {
      res.status(200).json({ message: 'Diagram saved' });
    } else {
      res.status(404).json({ message: 'Diagram for customer: '
       + req.params.KdNr + 'not found!'})
    }
  })
})

module.exports = router;

【问题讨论】:

    标签: node.js mongodb mongoose


    【解决方案1】:

    // $push 中的 $each 只允许一个值数组。所以你应该将你的数据对象更改为对象数组

    const data =[ {
          domain: req.body.domain,
          in: [{
            content : "New Graph",
            graph : {}
          }],
          out: [{
            content : "New Graph",
            graph : {}
          }]
      }]
    

    【讨论】:

    • 我试过你的解决方案,得到了这个:UnhandledPromiseRejectionWarning: MongoError: The field 'domains' must be an array but is of type object in document
    • { $push: { domain: { $each: [data] } } } 像这样使用
    • 同样的错误仍然存​​在 :-( 我也试过 :{ $addToSet: data }, or { $push: {"domains":data} },
    • domain:[ { name: { type: String, unique: true, required: true }, in: { content: { type: String }, graphSchema }, out: { content: { type: String }, graphSchema } }] 像这样改变你的模式
    • mhm...没用...我还需要将域作为数组而不是域...一个客户在“域”数组中保存了一个“域”数组
    猜你喜欢
    • 2022-12-02
    • 2021-11-23
    • 2021-05-05
    • 2023-02-25
    • 1970-01-01
    • 2022-11-20
    • 1970-01-01
    • 2022-12-01
    • 2021-10-06
    相关资源
    最近更新 更多