How can nodejs obtain the result feedback saved by mongoose?

  node.js, question

The purpose is to write a function to save a model separately, how to return the feedback in save?

var mongoOp = require('../models/basket');
 
 exports.insert = function (pname, pprice, edate) {
 Log (pname plus'' plus pprice);
 var db = new mongoOp();
 var response = {};
 db.pname = pname;
 db.pprice = pprice;
 db.edate = edate;
 db.save(function(err) {
 if (err) {
 response = {
 "error": true,
 "message": "Error adding data"
 };
 } else {
 response = {
 "error": false,
 "message": "Data added"
 };
 bracket
 });
 return err;
 };

If you use co and thunkify modules, how do you write them?

Because db.save () is asynchronous, yourreturn errWhat is returned isundefined. The correct approach here is to use callback functions, as follows:

exports.insert = function(pname, pprice, edate, cb) {
 //   ...
 db.save(function(err) {
 if (err) {
 response = {
 "error": true,
 "message": "Error adding data"
 };
 } else {
 response = {
 "error": false,
 "message": "Data added"
 };
 bracket
 return cb(err, response);
 });
 };

coAndthunkifyNot used.