我看到此问题已经发布过,但似乎无法联系并找到我的情况修复,我已将代码缩减为基本元素以深入研究问题,但尚无解决方案 - 它是我第一次使用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 : 'jesse@test.com',
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: 'jesse@test.com',
mobile: '*********',
activated: 'T',
password: 'Test',
_id: 54995052c17aa7470bfca9e1 }
提前致谢!
答案 0 :(得分:0)
您是否尝试过检查数据库中的内容? 您的查询
users.find({display_name:'jesse'})
与您保存的数据不匹配。 display_name:' J esse', 你能试试小写吗?
答案 1 :(得分:0)
好吧,我已经在网上遇到了类似的问题,以至于我没有看到我自己的实际错误,即使用的IP是错误的。使用过去项目中的相同云实例,但此后IP已刷新。
Woops!