FeatherJS错误消息:调用populate(目标,选项)现在已弃用

时间:2018-05-30 10:19:03

标签: node.js mongoose runtime-error

我正在使用Feathers JS框架和mongoose库来创建Web服务,但是当我想启动我的项目时,我收到此错误消息:

 Mongoose option.lean is false. Use hook.results.toObject() to convert the results to JS objects
Calling populate(target, options) is now DEPRECATED and will be removed in the future

代码:

  var mongooseService = require('feathers-mongoose');

 app.use('user', mongooseService({
     name: 'user',
     Model: require('./../models/user'),
     id: 'cuid'
   }))

   .use('post', mongooseService({
     name: 'post',
     Model: require('./../models/post'),
   }))

 var getLastPost = function() {
   return app.service('post').find({
     query: {
       "status": {
         $in: [0, 1, 2]
       },
       $select: ['version', '_id']
     }
   });
 };
 app.service('post')
   .before(getLastPost)
   .after(hooks.populate('user', {
     service: 'user'
   }))

我有什么办法要删除此错误消息吗?

2 个答案:

答案 0 :(得分:1)

定义服务时需要添加精益属性。

 var mongooseService = require('feathers-mongoose');

 app.use('user', mongooseService({
     "name": 'user',
     "Model": require('./../models/user'),
     "id": 'cuid',
     "lean": true
   }))

   .use('post', mongooseService({
     "name": 'post',
     "Model": require('./../models/post'),
     "lean": true
   }))

 var getLastPost = function() {
   return app.service('post').find({
     query: {
       "status": {
         $in: [0, 1, 2]
       },
       $select: ['version', '_id']
     }
   });
 };
 app.service('post')
   .before(getLastPost)
   .after(hooks.populate('user', {
     create: function(hook, next) {
        hook.result = hook.result.toObject();
        next();
     },
     service: 'user'
   }))

答案 1 :(得分:0)

修复第二条消息错误:

 var auk = require('feathers-hooks-common');
   var populate = function(serviceName,name,parentFieldName,childFieldName){
   return auk.populate({
      schema: {
      include: {
        service: serviceName,
        nameAs: name,
        parentField: parentFieldName,
        childField: childFieldName
      }
    }
  })
  }
  app.service('post').hooks({
    after: {all: populate("user","user","user","_id")}
  });