我正在使用MongoDB + Node.js创建应用程序,但是此代码收到错误:

company.js

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var companySchema = Schema({

    creator: { 
        type: Schema.Types.ObjectId, 
        ref: 'User' 
    },
    name: String,
    description: String,
    categories: [String],
    website: String,
    address: String,
    statuses: [{ 
        date: { type: Date, default: Date.now }, 
        status: String 
    }],
    friends: [{ 
        name: String, 
        description: String 
    }],
    priority: Number,
    isActive: Boolean,
    contacts: [{ 
        type: Schema.Types.ObjectId, 
        ref: 'ContactPerson' 
    }],
    createdOn: { type: Date, default: Date.now }
});

module.exports = mongoose.model('Company', companySchema);

api.js

api.route('/company')

    .post(function(req, res) {

        var company = new Company({
                creator: req.decoded.id,
                name: req.body.name,
                description: req.body.description,
                categories: req.body.categories,
                website: req.body.website,
                address: req.body.address,
                friends.name: req.body.friendName,
                statuses: { status: "New Company" },
                priority: req.body.priority,
        });

        company.save(function(err, newCompany) {

            if(err) {
                res.send(err);
                return
            }
            res.json({ message: "New company has been created!" });
        });
    })

错误

friends.name:req.body.friendName,

SyntaxError:意外令牌。

我也尝试过与friend ['name']进行尝试,但结果是相同的:SyntaxError:意外令牌[

请告诉我我的错误在哪里

===============>>#1 票数:3 已采纳

尝试:

var company = new Company({
        creator: req.decoded.id,
        name: req.body.name,
        description: req.body.description,
        categories: req.body.categories,
        website: req.body.website,
        address: req.body.address,
        friends: {
            name: req.body.friendName
        },
        statuses: { status: "New Company" },
        priority: req.body.priority,
});

要么:

var company = new Company({
        creator: req.decoded.id,
        name: req.body.name,
        description: req.body.description,
        categories: req.body.categories,
        website: req.body.website,
        address: req.body.address,
        'friends.name': req.body.friendName,
        statuses: { status: "New Company" },
        priority: req.body.priority,
});

取决于你所追求的。

  ask by Vintila translate from so

未解决问题?本站智能推荐: