我看到这个问题之前已经发布,但似乎无法涉及并找到我的情况的修复,我已将代码缩减为基本元素以深入研究问题,但尚无解决方案 - 这是我第一次用Mongoose保存记录,而不仅仅是查询,所以我敢肯定这是我的错,但我所尝试的是如此简单......请帮忙!快要疯了!猫鼬不保存
代码:
// Constants
var TRUSTED_ORIGIN = "";
var MONGO_SERVER = "mongodb://***.**.**.***/*********";
// Create the database schema
mongoose = require('mongoose');
Schema = mongoose.Schema;
mongoose.connect(MONGO_SERVER);
ObjectId = Schema.ObjectId;
//=============================================================
var UsersSchema = new Schema({
display_name : String,
email : String,
mobile : String,
activated : String,
password : String
});
//=============================================================
//models = require('./models/schemas.js')
var users = mongoose.model('Users',UsersSchema);
//=============================================================
function consoleLogger(msg) {
console.log("*********************************************************");
console.log(msg);
}
"use strict";
var myServer = "http://***.***.**.***/";
process.title = '***********';
// ---------------------------------------------------------------------------------------
// Test Components
consoleLogger("Preparing the first test");
var test1 = "";
console.log("Host: "+mongoose.connection.host);
console.log("Port: "+mongoose.connection.port);
var user_value = new users({display_name: 'Jesse',
email : '[email protected]',
mobile : '******',
activated : 'T',
password : 'Test'});
consoleLogger(user_value);
try {
user_value.save(function(err){
consoleLogger("Item Saved. Err: "+err);
users.find({display_name:'jesse'})
.exec(function(err,data){
consoleLogger("Item Found")
test1 = data[0].display_name
consoleLogger("test1 = "+test1);
});
});
}
catch (e) {
consoleLogger("Failed to save");
}
输出:
*********************************************************
Preparing the first test
Host: ***.**.**.***
Port: *****
*********************************************************
{ display_name: 'Jesse',
email: '[email protected]',
mobile: '*********',
activated: 'T',
password: 'Test',
_id: 54995052c17aa7470bfca9e1 }
提前感谢!
一个有效的观点,但是,检查集合以发现它不存在。主要需要通过记录“项目已保存”来保存它的验证,但事实并非如此。 – Jester