簡體   English   中英

需要有關 POST API 的幫助

[英]Need help regarding POST API

我有一個 mongoose 架構,如下所示,我正在嘗試創建一個新的俱樂部,但總是出現未定義的錯誤。 我檢查了很多次,但沒有成功。 有人可以幫我進行CRUD操作。 我是編程新手,盡我所能。

const schoolSchema = new mongoose.Schema
({
    schoolName: { type: String, unique: true, required: true },
    feePlan: {
        primary: { type: String, enum: ['Plan-A', 'Plan-B'], default: 'Plan-A', required: true },
        secondary: { type: String, enum: ['Plan-A', 'Plan-B'], default: '', },
    },
    schoolContact: {
        email:
            { type: String, lowercase: true, trim: true, index: true, unique: true, required: true },
        phonePrimary:
            { type: String, trim: true, unique: true, required: true },
        phoneSecondary:
            { type: String, trim: true },
        headInstructor:
            { type: String, required: true },
        websiteUrl: { type: String, trim: true, default: '' },
        businessAddress:
        {
            street: { type: String, required: true },
            city: { type: String, required: true },
            state: { type: String, required: true },
            zip: { type: String, required: true },
            country: { type: String, required: true },
        },
        otherAddress: {
            street: { type: String, required: true },
            city: { type: String, required: true },
            state: { type: String, required: true },
            zip: { type: String, required: true },
            country: { type: String, required: true },
        },

    },
    active: { type: Boolean, default: true },
    timestamps: { type: Date, deafault: true },
});
const schoolModel = mongoose.model('School', schoolSchema);
module.exports = schoolModel;

++++++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++++++++++++++++++++++++++

    router.post('/add', function (req, res, next) {
let schoolName = req.body.schoolName;
let feePlan = req.body.feePlan;
let primary = req.body.primary;
let secondary = req.body.secondary;
let schoolContact = req.body.schoolContact;
let email = req.body.email;
let phonePrimary = req.body.phonePrimary;
let phoneSecondary = req.body.phoneSecondary;
let headInstructor = req.body.headInstructor;
let websiteUrl = req.body.websiteUrl;
let businessAddress = req.body.businessAddress;
let street = req.body.street;
let city = req.body.city;
let state = req.body.state;
let zip = req.body.zip;
let country = req.body.country;
let otherAddress = req.body.otherAddress;
let streetOth = req.body.streetOth;
let cityOth = req.body.cityOth;
let stateOth = req.body.stateOth;
let zipOth = req.body.zipOth;
let countryOth = req.body.countryOth;

let schoolObj = new schoolModel({
    schoolName: schoolName,
    feePlan: {
        primary: primary,
        secondary: secondary,
    },
    schoolContact: {
        email: email,
        phonePrimary: phonePrimary,
        phoneSecondary: phoneSecondary,
        headInstructor: headInstructor,
        websiteUrl: websiteUrl,
        businessAddress:
        {
            street: street,
            city: city,
            state: state,
            zip: zip,
            country: country,
        },
        otherAddress: {
            streetOth: streetOth,
            cityOth: cityOth,
            stateOth: stateOth,
            zipOth: zipOth,
            countryOth: countryOth,
        },

    },
    active: { type: Boolean, default: true },
    timestamps: { type: Date, deafault: true },



});
schoolObj.save(function (err, schoolObj) {
    if (err) {
        res.send({ status: 500, message: 'Unable to Add school' });
        console.log(schoolObj);
    }
    else {
        res.send({ status: 200, message: 'school Added Successfully', schoolDetails: schoolObj });
    }
});
});

感謝 Justinas,以及所有看過的人。 我能夠解決這個問題。 我不需要聲明所有字段。 就在下面的解決方案有效。

    let schoolName = req.body.schoolName;
    let feePlan = req.body.feePlan;
    let schoolContact = req.body.schoolContact;

    let schoolObj = new schoolModel({
    schoolName: schoolName,
    feePlan: feePlan,
    schoolContact: schoolContact,

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM