These are chat archives for Automattic/mongoose

18th
Apr 2018
Nicholas Krause
@xerofoify
Apr 18 2018 00:49
Sorry to bug people but I posted some code someone gave me and would like someone to look at it if possible.
panigrah
@panigrah
Apr 18 2018 03:05
Try using User.create() instead?
Nicholas Krause
@xerofoify
Apr 18 2018 05:17
That doesn't work and it seems to be a race between connect and it not being called before that line as when I do two prints in both functions. One prints and the other doesn't, double checked by removing them. The person who gave me this code thought it worked but clearly did not assume for a race condition.
panigrah
@panigrah
Apr 18 2018 05:45
@xerofoify how are you calling registerUser? you will have to wait for the connect to complete before "User" is available to you. wherever you are initializing this you likely have const auth = require('this file")(MongoString) and then calling auth.connect?
if so you should wait for the auth.connect to resolve before making a call to auth.registerUser or anything else. Usually I do
await auth.connect();
// now should be connected - can catch error if promise rejects
auth.registerUser().then().....
Nicholas Krause
@xerofoify
Apr 18 2018 12:16
@panigrah this is my code I am no idea what you mean https://pastebin.com/5jL0QFP4 and it was given to me. How do I get connect to call before the var line: var newUser = new User(userData);
Does anyone else have ideas to fix this https://pastebin.com/5jL0QFP4 and it was given to me. How do I get connect to call before the var line: var newUser = new User(userData);
Kev
@lineus
Apr 18 2018 12:23
@xerofoify Can you create a 2nd pastebin with the file that you are importing this module into? So we can see how you are using it.
is the main server file. If you need anything else let me know.
Kev
@lineus
Apr 18 2018 12:32
in the server.js file, you're calling connect on data, but never on dataAuth.
so mongoose doesn't have a connection associated with the /users database
Nicholas Krause
@xerofoify
Apr 18 2018 12:33
How is that possible I am looking at that file now
Kev
@lineus
Apr 18 2018 12:34
const dataService = require("./data-service.js");
const dataServiceAuth = require("./data-service-auth.js");

const data = dataService(mongoDBConnectionString);
const dataAuth = dataServiceAuth(mongoDBUserConnectionString);

...

data.connect().then(()=>{
    app.listen(HTTP_PORT, ()=>{console.log("API listening on: " + HTTP_PORT)});
})
you have 2 separate connections defined in your server.js, one for each database. You only ever call connect() on the first one.
Nicholas Krause
@xerofoify
Apr 18 2018 12:38
dataAuth.connect().then(()=>{
app.listen(HTTP_PORT, ()=>{console.log("API listening on: " + HTTP_PORT)});
})
.catch((err)=>{
console.log("unable to start the server: " + err);
process.exit();
});
was added and it does not fix it
Kev
@lineus
Apr 18 2018 12:39
out of curiosity, are you following a tutorial?
Nicholas Krause
@xerofoify
Apr 18 2018 12:40
No someone gave me broken code and is assuming it isn't broken when it is
Kev
@lineus
Apr 18 2018 12:44
are data-service.js and data-service-auth.js pretty much exactly the same?
Nicholas Krause
@xerofoify
Apr 18 2018 12:45
Yes pretty much one is just for user accounts
Madhusudhan Srinivasa
@madhums
Apr 18 2018 12:46

Hi, I am indexing some fields in a collection.

For example:

var Companies = new Schema({ name: String, description: String });
var Products = new Schema({ name: String, description: String });

Companies.path('name').index({ text: true });
Companies.path('description').index({ text: true });
Products.path('name').index({ text: true });
Products.path('description').index({ text: true });

I get MongoError saying Index already exists with different options. Is this because the name of the indexes are same? If so, is there a way to provide a name for the index?

Kev
@lineus
Apr 18 2018 12:47
@xerofoify have you tried it with both connects at the same time? Like this:
data.connect().then(()=>{
  dataAuth.connect().then(() => {
    app.listen(HTTP_PORT, ()=>{console.log("API listening on: " + HTTP_PORT)});
  })
})
@madhums If I recall correctly, you can't have two fields as text indexes on the same collection simultaneously.
Nicholas Krause
@xerofoify
Apr 18 2018 12:50
Nope https://secret-citadel-17150.herokuapp.com/register here is the url and the data {
"userName": "bob@gmail.com",
"password": "Password12",
"password2": "Password12",
"fullName": "User Accounts",
"role": "users"
}
It still fails with postman
Kev
@lineus
Apr 18 2018 12:50
@madhums it's in the mongodb docs here
@xerofoify is it the same User constructor error?
Nicholas Krause
@xerofoify
Apr 18 2018 12:52
Can it is
*yes
Kev
@lineus
Apr 18 2018 12:56
ok, try one more thing, in the connect function of data-service-auth, add User to the resolve callback resolve(User)
Nicholas Krause
@xerofoify
Apr 18 2018 12:56
Actually it's the something else I will fix the build errors and see what happens after
Works now thanks
Nathaniel Suchy
@nsuchy
Apr 18 2018 13:18
Can I add a sub-document to a sub-document?
Will it have any performance issues?
Madhusudhan Srinivasa
@madhums
Apr 18 2018 13:18
@lineus Thank you!
Kev
@lineus
Apr 18 2018 14:08
@madhums you're welcome :)
@nsuchy yes you can nest subdocs, not aware of any performance issues inherent to doing so.
Nathaniel Suchy
@nsuchy
Apr 18 2018 19:39
@lineus So I want to let you know a bit more about my use case. A website I develop will allow users to have conversations (basically chat), the data structure is Conversation Document containing an array of user ids, and an array of message batch subdocuments, each message batch subdocument has up to 100 subdocuments containing the actual messages. Am I overthinking this?
I don't want a document to get too big to deal with
And I want users to have message history forever
Idk if I'm overthinking it
Nathaniel Suchy
@nsuchy
Apr 18 2018 19:48
// Conversations.js
'use strict';
const mongoose = require('mongoose');

const Msg = new mongoose.Schema({
  UserID: {
    type: String,
    required: true,
  },
  Content: {
    type: String,
    required: true,
  },
});

/*
Each Message Batch should contain a group of 100 messages.
Chat Clients should request the newest batch first.
As needed clients should request more batches.
*/
const MsgBatch = new mongoose.Schema({
  Msges: [Msg],
});

const ConversationSchema = new mongoose.Schema({
  UserIDs: {
    type: Array,
    required: true,
  },
  MsgBatches: [MsgBatch],
});

const Conversation = mongoose.model('Conversations', ConversationSchema);
module.exports = Conversation;

// From user.js
ConversationIDs: {
      type: Array,
      required: true,
      default: [],
    },
tibetoine
@tibetoine
Apr 18 2018 20:38
Hi. I'm working in Express with Mongoose, and i'm looking for a way to create a document from a complex Document get from an other API ? Could you help me with that ?
tibetoine
@tibetoine
Apr 18 2018 20:46
Ok I think, that this is my answer : http://mongoosejs.com/docs/guide.html#strict
Nathaniel Suchy
@nsuchy
Apr 18 2018 21:53
@tibetoine Feel free to DM me if you'd like to go into detail
I'd be happy to help