javascript - mongoose findall return data from a different model -


in users.js:

var mongoose = require('mongoose');  var user = mongoose.model('user', {   username: {     type: string,     required: true,     unique: true,     lowercase: true,   },     tasks: [{     type: mongoose.schema.types.objectid,     ref: 'tasks',   }], }); module.exports = user; 

i add newuser named user1 , save mongo.

the mongo doc looks like:

{      "_id" : objectid("574fb94f6a1e7d1826c16058"),     "username" :  "user1",     "tasks" : [ ],     "__v" :  0 } 

then try fetch document , works fine in handler:

in handlera.js:

var user = require('../models/users.js');  module.exports.getuser = function(req, res){   user.findone({username: "user1"}, function(err, data){     if(err){       console.log('getuser err', err);       res.send('error')     } else {       console.log('getuser fx success = ', err, data);       res.send(data)     }   }); }; 

the result of console.log:

getuser fx success = null { tasks: [], __v: 0, username: 'user1', _id: 574fb94f6a1e7d1826c16058 }


but same code fails in other handler in separate file.

in handlerb.js:

var user = require('../models/users.js');  module.exports.addstuff = function(req, res){    user.findone({username: "user1"}, function(err, data){     if(err){       console.log('addstuff err', err);       res.send('error')     } else {       console.log('addstuff fx success =', err, data);       res.send(data)     }   }); }; 

the result of console.log:

addstuff fx success null null

tried....and failed....

i tried other solution question: mongoose query return null

mongoose pluralizes model names it's running find on "blogposts" collection instead of "blogpost". said, query in mongo shell on "blogmodel" collection. in case:

var blogmodel = mongoose.model("blogmodel", ..) 

or pass collection name third param:

var blogmodel = mongoose.model("blogpost", schema, "blogmodel") 

this solution results in handlera.js returning null document handlerb.js.

thanks time. appreciated.


addendum. ran find({}) under both user , tasks models in handlerb.js returned document in both cases based on tasks model. see console.logs below user.find({}) , tasks.find({}) err null value data.

how badly have broken things? how can model.find return data not in model?

user.find({},funtion(err, data) ____________________ console.log of err  data null  [ { subtasks: [],     team: [ [object] ],     __v: 0,     maintask: '1',     _id: 574fce63d744cba421f750c1    },   { subtasks: [],     team: [ [object] ],     __v: 0,     maintask: '2',     _id: 574fce65d744cba421f750c2    } ]  tasks.find({},function(err, data) ___________________  console.log of err data null  [ { subtasks: [],     team: [ [object] ],     __v: 0,     maintask: '1',     _id: 574fce63d744cba421f750c1 },   { subtasks: [],     team: [ [object] ],     __v: 0,     maintask: '2',     _id: 574fce65d744cba421f750c2    }, ] 

this tasks model... tasks.js

var mongoose = require('mongoose');  var subtaskschema = new mongoose.schema({    subtask: {    type: string,   },   team: {     type: array,     'defualt': [],   },   done: {     type: boolean,     'defualt': false,   }, });  var tasks = mongoose.model('tasks', {   maintask: {     type: string,   },   subtasks: {     type: [subtaskschema],   },   team: {     type: array,     'defualt': [],   },   done: {     type: boolean,     'defualt': false,   }, });  module.exports = tasks; 

the mongoose docs suggest define model so, did try this?

var schema = new mongoose.schema({ name: 'string', size: 'string' }); var tank = mongoose.model('tank', schema); 

Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo