TypeError:不能在'中使用'运营商搜索' _id'在[mongoose,body-parser,。/ config]中

时间:2017-07-26 20:16:46

标签: node.js mongoose mongoose-schema

我一直收到错误消息" TypeError:无法在'中使用'运营商搜索' _id' in" +" mongoose"," body-parser"," ./ config" ......显然任何依赖。我试过了:

所以现在我很难过。

这是我的用户模型:

"use strict";

var mongoose = require('mongoose');

var userSchema = new mongoose.Schema({
    username: {
        type: String,
        required: false},
    password: {
        type: String,
        required: false}
});

var user = mongoose.model('user', userSchema);

module.exports = user;

这是我的server.js:

"use strict";

var require = require('./models/user');
//var bodyParser = require('body-parser');
//var config = require('./config');
var mongoose = require('mongoose');
var express = require('express');
var app = express();
//app.use(bodyParser.json());
app.use(express.static('public'));

var runServer = function (callback) {
    mongoose.connect(config.DATABASE_URL, function (err) {
        if (err && callback) {
            return callback(err);
        }

        app.listen(config.PORT, function () {
            console.log('Listening on localhost:' + config.PORT);
            if (callback) {
                callback();
            }
        });
    });
};

if (require.main === module) {
    runServer(function (err) {
        if (err) {
            console.error(err);
        }
    });
};


app.post('/login', function(req, res) {
    console.log(req);
    //console.log(req.body.password);
    user.create({
        username: 'Test',
        password: '1234'
        //username: req.body.username,
        //password: req.body.password
    }, function (err, user) {
        if(err) {
            return res.status(500).json({
                message: 'Internal Server Error'
            });
        }
        res.status(201).json(user);
        console.log(user);
    });
});

exports.app = app;
exports.runServer = runServer;

app.listen(process.env.PORT || 8080);

这是我的config.js文件:

exports.DATABASE_URL = process.env.DATABASE_URL || global.DATABASE_URL 
|| 'mongodb://demo:demo@ds121483.mlab.com:21483/job-tracker-data';
//exports.TEST_DATABASE_URL = process.env.TEST_DATABASE_URL ||
    //'mongodb://demo:demo@ds121483.mlab.com:21483/job-tracker-data';
exports.PORT = process.env.PORT || 8080;

以下是错误消息的示例:

/home/meryem/DevStuff/code/job-tracker-capstone-
app/node_modules/mongoose/lib/document.js:212
      if (obj && '_id' in obj) {
                          ^

TypeError: Cannot use 'in' operator to search for '_id' in ./config
    at model.Document.$__buildDoc (/home/meryem/DevStuff/code/job-
tracker-capstone-app/node_modules/mongoose/lib/document.js:212:27)
    at model.Document (/home/meryem/DevStuff/code/job-tracker-
capstone-app/node_modules/mongoose/lib/document.js:61:20)
    at model.Model (/home/meryem/DevStuff/code/job-tracker-capstone-
app/node_modules/mongoose/lib/model.js:46:12)
    at new model (/home/meryem/DevStuff/code/job-tracker-capstone-
app/node_modules/mongoose/lib/model.js:3671:13)
    at model (/home/meryem/DevStuff/code/job-tracker-capstone-
app/node_modules/mongoose/lib/model.js:3669:16)
    at Object.<anonymous> (/home/meryem/DevStuff/code/job-tracker-
capstone-app/server.js:5:14)
    at Module._compile (module.js:413:34)
    at Object.Module._extensions..js (module.js:422:10)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Function.Module.runMain (module.js:447:10)
    at startup (node.js:148:18)
    at node.js:405:3

TypeError: Cannot use 'in' operator to search for '_id' in body-parser
    at model.Document.$__buildDoc (/home/meryem/DevStuff/code/job-
tracker-capstone-app/node_modules/mongoose/lib/document.js:212:27)
    at model.Document (/home/meryem/DevStuff/code/job-tracker-
capstone-app/node_modules/mongoose/lib/document.js:61:20)
    at model.Model (/home/meryem/DevStuff/code/job-tracker-capstone-
app/node_modules/mongoose/lib/model.js:46:12)
    at new model (/home/meryem/DevStuff/code/job-tracker-capstone-
app/node_modules/mongoose/lib/model.js:3671:13)
    at model (/home/meryem/DevStuff/code/job-tracker-capstone-
app/node_modules/mongoose/lib/model.js:3669:16)
    at Object.<anonymous> (/home/meryem/DevStuff/code/job-tracker-
capstone-app/server.js:4:18)
    at Module._compile (module.js:413:34)
    at Object.Module._extensions..js (module.js:422:10)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Function.Module.runMain (module.js:447:10)
    at startup (node.js:148:18)
    at node.js:405:3

TypeError: Cannot use 'in' operator to search for '_id' in mongoose
    at model.Document.$__buildDoc (/home/meryem/DevStuff/code/job-
tracker-capstone-app/node_modules/mongoose/lib/document.js:212:27)
    at model.Document (/home/meryem/DevStuff/code/job-tracker-
capstone-app/node_modules/mongoose/lib/document.js:61:20)
    at model.Model (/home/meryem/DevStuff/code/job-tracker-capstone-
app/node_modules/mongoose/lib/model.js:46:12)
    at new model (/home/meryem/DevStuff/code/job-tracker-capstone-
app/node_modules/mongoose/lib/model.js:3671:13)
    at model (/home/meryem/DevStuff/code/job-tracker-capstone-
app/node_modules/mongoose/lib/model.js:3669:16)
    at Object.<anonymous> (/home/meryem/DevStuff/code/job-tracker-
capstone-app/server.js:6:16)
    at Module._compile (module.js:413:34)
    at Object.Module._extensions..js (module.js:422:10)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Function.Module.runMain (module.js:447:10)
    at startup (node.js:148:18)
    at node.js:405:3

请帮忙。

0 个答案:

没有答案