【问题标题】:How to map data using Mongoose Multiple Schema? I need to do this dynamically? Does anyone have an example?如何使用 Mongoose Multiple Schema 映射数据?我需要动态执行此操作吗?有人有例子吗?
【发布时间】:2019-09-27 17:16:30
【问题描述】:

我需要实现一个代码,它支持将数据从 mongoose 路由到 mongodb 和 node js 的映射。我需要它本质上是动态的,以便它支持 REST 调用。

【问题讨论】:

  • 这太宽泛了。请更具体并添加现有代码。

标签: node.js mongodb mongoose-schema


【解决方案1】:

我有你的问题的答案。我有可以帮助你的代码。 你可以从这里查看它的教程:YouTube tutorial 或者直接从这里下载代码:GitHub Link 它也可以在没有 Panther 的情况下工作,您只需要正确映射即可。

对于初学者:-

var ProfileDataSchema = new Schema({
  hobby: {type: String},
  graduate: {type: String},
  webseries: {type: String},
  tvseries: {type: String},
  name: {type: String, unique: true},
  gaming: {type: String},
  movie: {type: String}
}, {collection: 'profile-data'});

var ProfileData = mongoose.model('ProfileData', ProfileDataSchema);
ProfileDataSchema.plugin(uniqueValidator);

app.use(session({secret:'mysession',resave:false,saveUninitialized:true}));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

router.get('/login_router_get', function(req, res, next) {
    res.sendFile(path.join(__dirname + '/login.html'));
  });

  router.post('/login_router_post', function(req, res, next) {

  console.log('IN POST');
  var user_name = req.session.user_name;
              var passwordtxt = req.session.passwordtxt;
              console.log('IN LOGIN ROUTER POST SESSION VALUE'+req.session.user_name);
              request({

              type: 'POST',
              url: 'http://10.0.10.176:9080/PEGServer/subhan.lib/login_session/get_data', 
              async: false,
              data: { user_name : user_name, passwordtxt : passwordtxt }, 
              dataType: 'json'

              },
               function(err, res, body){
              var json = JSON.parse(body);
              console.log(json+"in ajax call");
              console.log(json.name);
              var item = json;
               //var item ={body};

console.log(item);
console.log('Item inserted');

  var data = new ProfileData(item);
  data.save();

});
res.json(user_name);
    });

【讨论】:

  • 当外部链接被删除/更改时,只有链接的答案将失效。您应该在此处发布带有详细信息的答案并提供其他链接
  • 我添加链接只是因为我已经分享了关于 YouTube 和 GitHub 链接的完整详细信息,但我不会从现在开始。
猜你喜欢
  • 1970-01-01
  • 2020-11-05
  • 2010-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多