首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >后端未初始化数组

后端未初始化数组
EN

Stack Overflow用户
提问于 2019-07-28 03:11:09
回答 1查看 89关注 0票数 2

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

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

代码语言:javascript
复制
   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: "" }]
          };

在后端,这是mongoose模型:

代码语言:javascript
复制
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);
EN

回答 1

Stack Overflow用户

发布于 2019-07-28 03:58:08

定义vouchers有一个对象数组

代码语言:javascript
复制
vouchers: [{
    type: [Object], // There is no Schema Type in moongoose, change to [String]
    status: [String]
}]

不要担心你的vouchers: [{ companyId: "", companyname: "" }]

您想要使用companyIdcompanyname

更改架构..

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57235282

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档