简体   繁体   English

猫鼬不保存

[英]Mongoose Not Saving

I see this question has been posted before, but can't seem to relate and find a fix for my situation, I have reduced the code down to basic elements to drill down on the issue, but no solutions yet - it is the first time I've used Mongoose to save records, and not only to query, so I'm sure this is my fault, but what I'm trying is so simple... Please help! 我看到这个问题已经发布过,但是似乎无法解决我的问题,我已将代码缩减为基本元素以深入研究该问题,但尚无解决方案-这是第一次我用过Mongoose来保存记录,不仅用于查询,所以我确定这是我的错,但是我要尝试的操作是如此简单...请帮助! Going crazy! 快要疯了!

Code: 码:

// 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");
}

Output: 输出:

*********************************************************
Preparing the first test
Host: ***.**.**.***
Port: *****
*********************************************************
{ display_name: 'Jesse',
  email: 'jesse@test.com',
  mobile: '*********',
  activated: 'T',
  password: 'Test',
  _id: 54995052c17aa7470bfca9e1 }

Thanks in advance! 提前致谢!

Have you tried checking what is in database? 您是否尝试过检查数据库中的内容? Your query 您的查询

users.find({display_name:'jesse'})

does not match data you saved. 与您保存的数据不匹配。 display_name: ' J esse', Could you try with lowercase? display_name:' J esse',可以尝试使用小写字母吗?

Ok, well I have gotten so caught up with similar issues online that I failed to see right past the actual fault of my own being that the IP to use was wrong. 好吧,好吧,我已经在网上陷入了类似的问题,以至于我无法克服自己的实际过失,即所使用的IP是错误的。 Was using the same cloud instance from a past project but since then the IP had been refreshed. 从过去的项目中使用的是相同的云实例,但此后IP已刷新。

Woops! 哇!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM