Promise 中的类型错误 [Node.js]

TypeError in Promises [Node.js]

我正在为 GET、POST、PUT 和 DELETE 执行 REST API,现在我正在执行 POST:

restAPI.js:

var restApi = {
  POST: function(options) {
     if (options.method !== 'POST') {
       console.log('Method must be POST!');
     }

     var entry = new options.model(options.data);
     return entry.save;

  };
};

module.exports = restApi;

现在我可以在我的控制器中要求和使用了。我正在使用 MongoDB 和猫鼬。

siteController.js:

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

var siteController = {
 create: function(req, res, next) {
    var data = req.body;

    var options = {
      method: 'POST',
      model: Booking,
      data: data
    };


    restAPI.POST(options).then(function () {
      res.redirect('/obrigado/' + req.cookies.locale);
    }).then(undefined, function(error) {
      console.log(error);
    });
 }
};

module.exports = siteController;

但返回以下错误:

TypeError: Object function (options, fn) {

 if ('function' == typeof options) {
   fn = options;
   options = undefined;
 }

 if (!options) {
   options = {};
 }

 var self = this;
 var finalPromise = new Promise(fn);

 // Check for preSave errors
 var preSaveErr = self.$__presaveValidate();
 if (preSaveErr) {
   finalPromise.reject(preSaveErr);
   return finalPromise;
 }

 // Validate
 var p0 = new Promise;
 if (this.schema.options.validateBeforeSave) {
   p0 = self.validate();
 }

 // Call save hooks on subdocs
 var p1 = p0.all(function () {
   var subDocs = self.$__getAllSubdocs();
   return subDocs.map(function (d) {return d.save();});
 });

 // Handle save and resaults
 p1
   .then(this.$__handleSave.bind(this, options))
   .then(function (result) {
     self.$__reset();
     self.$__storeShard();

     var numAffected = 0;
     if (result)
       numAffected = (Array.isArray(result)) ? result.length : result;
     // was this an update that required a version bump?
     if (self.$__.version && !self.$__.inserting) {
       var doIncrement = VERSION_INC === (VERSION_INC & self.$__.version);
       self.$__.version = undefined;

       if (numAffected <= 0) {
         // the update failed. pass an error back
         return finalPromise.reject(new VersionError);
       }

       // increment version if was successful
       if (doIncrement) {
         var key = self.schema.options.versionKey;
         var version = self.getValue(key) | 0;
         self.setValue(key, version + 1);
       }
     }

     self.emit('save', self, numAffected);
     return finalPromise.fulfill(self, numAffected);
   }
   , function (err) {
     // If the initial insert fails provide a second chance.
     // (If we did this all the time we would break updates)
     if (self.$__.inserting) {
       self.isNew = true;
       self.emit('isNew', true);
     }
     finalPromise.reject(err);
   })
   .end();

 return finalPromise;
} has no method 'then'
   at siteController.booking.create (/home/ubuntu/workspace/front-back/app/app/server/controllers/site.js:84:29)
   at Layer.handle [as handle_request] (/home/ubuntu/workspace/front-back/node_modules/express/lib/router/layer.js:82:5)
   at next (/home/ubuntu/workspace/front-back/node_modules/express/lib/router/route.js:100:13)
   at exports.setLocale (/home/ubuntu/workspace/front-back/app/app/server/middleware/index.js:8:3)
   at Layer.handle [as handle_request] (/home/ubuntu/workspace/front-back/node_modules/express/lib/router/layer.js:82:5)
   at next (/home/ubuntu/workspace/front-back/node_modules/express/lib/router/route.js:100:13)
   at Route.dispatch (/home/ubuntu/workspace/front-back/node_modules/express/lib/router/route.js:81:3)
   at Layer.handle [as handle_request] (/home/ubuntu/workspace/front-back/node_modules/express/lib/router/layer.js:82:5)
   at /home/ubuntu/workspace/front-back/node_modules/express/lib/router/index.js:234:24
   at Function.proto.process_params (/home/ubuntu/workspace/front-back/node_modules/express/lib/router/index.js:312:12)

我做错了什么?谁能帮帮我?

您的 .POST 方法正在返回 entry.save,这是一种方法,而不是承诺,这实际上就是错误消息告诉您的内容。意思是那个大函数没有 .then 属性。

您需要调用它:

var entry = new options.model(options.data);
return entry.save( /* provide any needed arguments here */ );