SyntaxError:意外的令牌

时间:2015-05-17 10:36:03

标签: javascript node.js mongodb express

我正在使用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:意外的令牌。

我也尝试过与朋友['name']但结果是一样的:SyntaxError:意外的令牌[

请告诉我我的错误在哪里

1 个答案:

答案 0 :(得分: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,
});

取决于你所追求的目标。