简体   繁体   中英

Named object inside an array inside of an object

I have a node.js model, in which I have an array named keys. Inside this array I have a bunch of objects. I need to get these objects named in the view.

Here's the model:

var mongoose = require('mongoose');

var website = require('./website');

var plm = require('passport-local-mongoose');

var accountSchema = new mongoose.Schema({
  isPremium: Boolean,
  accType: String,
  websites: [],
  keys: []
});

accountSchema.plugin(plm);

module.exports = mongoose.model('Account', accountSchema);

And here's the view:

<% include ./../partials/header.ejs %>
  <h1>
    <%= title %>
  </h1>
  <table class="table table-striped table-hover">
    <tr>
      <th>User</th>
      <% if (user) { %>
        <th>Actions</th>
        <% } %>
    </tr>
    <% for(let i=0; i<users.length; i++){ let user = users[i] %>
      <tr>
        <td>
          <%= user.username %>
        </td>
        <td>
          <%= user.keys[i].name %>
        </td>
        <td>
          <a href="/admin/edit/<%= i %>" class="btn btn-info">Edit</a>
          <%#<a href="/admin/delete/i" class="btn btn-danger confirmation">Delete</a>%>
        </td>
      </tr>
      <% } %>
  </table>
  <% include ./../partials/footer.ejs %>

I am trying to populate the keys during the registration like this:

/* POST register */
router.post('/register', function(req, res, next) {
  // use the Account model to create a new user with passport
  Account.register(
    new Account({
      username: req.body.username,
      isPremium: false,
      websites: [
        req.body.websiteOwned,
        req.body.websiteCompet1,
        req.body.websiteCompet2,
      ],
      keys: [
        { name: 'Google', key: '' },
        { name: 'Built With', key: '' },
        { name: 'Check Host', key: '' },
        { name: 'Alexa', key: '' },
        { name: 'Facebook', key: '' },
        { name: 'Instagram', key: '' },
        { name: 'Moz', key: '' },
      ], // accType: 'admin',
    }),
    req.body.password,
    function(err, account) {
      if (err) {
        // failure
        console.log(err);
        res.redirect('error', { title: 'Create Account Error' });
      }
      res.redirect('/login'); // success
    }
  );
});

There are a couple of things I don't understand. When I register like this, all it prints in the view is: Object object .

When my model is like this:

var accountSchema = new mongoose.Schema({
  isPremium: Boolean,
  accType: String,
  websites: [],
  keys: [
    { name: String, key: String },
    { name: String, key: String },
    { name: String, key: String },
    { name: String, key: String },
    { name: String, key: String },
    { name: String, key: String },
    { name: String, key: String },
  ],
});

It prints out { name: 'Google', key: '' } which is ok because I have one user and i is 0. But when I edit my view to this:

<% for(let i=0; i<users.length; i++){ let user = users[i]; %>
  <tr>
    <td>
      <%= user.username %>
    </td>
    <td>
    <% for(let j=0; j<keys.length; j++) { %>
      <%= user.keys[j] %>
    <% } %>
    </td>
    <td>
      <a href="/admin/edit/<%= i %>" class="btn btn-info">Edit</a>
      <%#<a href="/admin/delete/i" class="btn btn-danger confirmation">Delete</a>%>
    </td>
  </tr>
  <% } %>

So I added a loop inside, it says keys is not defined . So I'm guessing I'm messing something with scope?

My second issue is: When I'm trying to access a property of an object in the keys array like this: user.keys[i].name I get this error: Cannot read property 'name' of undefined .

What am I doing wrong and how do I fix it? Is it possible to use the first model( keys=[] ) in implementation? It would be more future-proof.

I have no idea why, but when after I deleted and added it a couple of times, mongoose suddenly added the objectId field to all 7 objects in the array. It works fine with objectId . I would appreciate an explanation if anyone understands what happened here.

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