【问题标题】:Array not initialized in the backend数组未在后端初始化
【发布时间】:2019-12-05 16:45:46
【问题描述】:

我正在尝试在注册时发送用户对象。该用户对象具有许多属性,这些属性被发送到后端并正确添加到数据库中。但是,有一个属性,一个名为vouchers 的数组,当我添加它时,用户对象不会发送到数据库。

这是从离子前端发送到后端的用户对象的代码:

   let userobj = {
            username: this.username,
            name: this.name,
            region: this.region,
            building: this.building,
            password: this.password,
            confirmPassword: this.confirmpassword,
            points: 300,
            vouchers: [{ companyId: "", companyname: "" }]
          };

在后端,这是猫鼬模型:

const mongoose = require("mongoose");
var Voucher = mongoose.Schema.Types.Voucher;

const userSchema = mongoose.Schema({
  username: {
    type: String,
    required: true,
    trim: true,
    lowercase: true
  },
  password: {
    type: String,
    required: true,
    trim: true
  },
  name: {
    type: String,
    required: true,
    trim: true
  },
  region: {
    type: String,
    required: true,
    trim: true
  },
  building: {
    type: Number,
    required: true,
    min: 0
  },
  points: {
    type: Number,
    required: false,
    min: 0
  },
  vouchers: {
    type: [Object],
    status: [String],
    required: false,
    default: []
  },

  createdAt: {
    type: Date,
    default: Date.now
  },
  updatedAt: Date
});

mongoose.model("User", userSchema);

【问题讨论】:

    标签: node.js angular mongodb ionic-framework


    【解决方案1】:

    定义vouchers 有一个对象数组

    vouchers: [{
        type: [Object], // There is no Schema Type in moongoose, change to [String]
        status: [String]
    }]
    

    不知道你的vouchers: [{ companyId: "", companyname: "" }]

    您想使用companyIdcompanyname

    更改架构..

    vouchers: [{ companyId: String, companyname: String }]

    【讨论】:

      猜你喜欢
      • 2015-10-03
      • 2021-12-01
      • 2018-10-17
      • 2014-11-30
      • 1970-01-01
      • 1970-01-01
      • 2013-12-01
      • 2012-01-26
      • 2020-10-24
      相关资源
      最近更新 更多