我看到這個問題之前已經發布,但似乎無法涉及並找到我的情況的修復,我已將代碼縮減爲基本元素以深入研究問題,但尚無解決方案 - 這是我第一次用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