简体   繁体   中英

Adding to mongoose nested document

I'm trying to setup a nested mongoose document as such:
models/data.js

var mongoose = require('mongoose');

var addresses = new mongoose.Schema({
  "street": String,
  "city": String,
  "state": String,
  "zip": Number,
});

var recipes = new mongoose.Schema({
  "recipe1": String,
  "recipe2": String,
  "recipe3": String,
  "recipe4": String,
  "recipe5": String,
});

var home = new mongoose.Schema({
  "name": String,
  "image": String,
  "recipe": [recipes],
});

var away = new mongoose.Schema({
  "name": String,
  "image": String,
  "recipe": [recipes],
  "address": [addresses],
});

var foods = new mongoose.Schema({
  "home": [home],
  "away": [away],
});

var users = new mongoose.Schema({
"username": String,
"firstname": String,
"lastname": String,
"email": String,
"password": String,
"address": [addresses],
});

var data = new mongoose.Schema({
  "users": [users],
  "foods": [foods],
});

exports.Data = mongoose.model('data', data);

add.js

var data = require('../models/data.js');

exports.update = function(req, res){
  // create a user in a Room
  var data = new data.Data();

  data.users.push({ username: 'Joe' });

  data.save(function (err) {
    if (!err) console.log('Success!');
  });
};

What I am trying to do is add a name into into the nested document. But when I run the program and submit my form from the client side(jquery mobile), I get an error that says:

TypeError: Cannot read property 'Data' of undefined
    at exports.update (..\routes\add.js:5:20)
    at callbacks (..\node_modules\express\lib\router\index.js:164:37)
    at param (..\node_modules\express\lib\router\index.js:138:11)
    at pass (..\node_modules\express\lib\router\index.js:145:5)
    at Router._dispatch (..\node_modules\express\lib\router\index.js:173:5)
    at Object.router (..\node_modules\express\lib\router\index.js:33:10)
    at next (..\node_modules\express\node_modules\connect\lib\proto.js:174:15)
    at Immediate.<anonymous> (..\node_modules\express\node_modules\connect\node_modules\express-session\index.js:433:7)
    at Immediate.immediate._onImmediate (timers.js:440:18)
    at processImmediate [as _immediateCallback] (timers.js:383:17)

I would like to know what I'm doing wrong. Thank you!

In your add.js file you are overwriting the data variable in the outer scope and making it inaccessible.

Here is a simple example showing the issue:

var data = { x: 1 } // define data in an outer scope

function foo() {
  // the next line overwrites the outer scope data var, causing an error
  var data = data.x
  console.write(data)
}

foo()

To fix the issue, update your app.js file to look something like this:

var Data = require('../models/data.js').Data;

exports.update = function(req, res){
  // create a user in a Room
  var data = new Data();

  data.users.push({ username: 'Joe' });

  data.save(function (err) {
    if (!err) console.log('Success!');
  });
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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