My goal is to include a new country (with increased country_id) in DB if it does not exist. In that situation, I am trying to find the country's largest country ID and try to enter a new country with country_id + 1. Otherwise I do not do anything The readFile readfile, filetoArray change a promise that file content in an array that is, Prosesmap store in Mongodb what we know works projects each array element and fixed or No The problem is: always gives me the same result, whenever any data first the same is inserted into the database ...
Promise. Launch (Country.FindOn ({}). Sort ({'zid': -1}). Exec ()
var mongoose = require ( 'mongoose'); var schema = mongoose.Schema; var countrySchema = new schema ( {Zn: {type: string}, essential: true}, zid: {type: number, essential: true}}); Wired promise = required ('bluebird'); Function Processmap (Data) {Return Promise. Bid (data) Then (insertCountry);} var insertCountry = function () {var googledata = this; // Tie the promise back from the PR Omise.promisifyAll (Country.findOne ({zn: googledata.country}) .exec ()). Then (function (dbdata) {return {dbdata: dbdata, googledata: googledata};}) .then (insertCountryIfNotExist)} var insertCountryIfNotExist = function (data) {return promise.promisifyAll (country.findOne ({}). Sort ({ 'zid': -1}). Exec ()) .then (function (D) {var newc = new country ({zn: data.googledata.country, zid: d.zid + 1}); return promise. Pramosting All (newc.saveAsync ())}); } // The main code is read here file (file). Then (filetoArray) .map (processMap, {concurrency: 1}) // after that end. Then (function (data) console.log ('done');} Actually returns inherited from Exec, there is no need to use blubber on your case.
Or if you want to use blubber, do not mix Mongoose promise with blue bird.
some examples:
var insertCountry = function () {var googledata = this; return Deshkfndon ({zn: googledata.country}). Exec (). then (function (dbdata) { Return {dbdata: dbdata, googledata: googledata}}}. Then (function (data) {return Country.fi ndOne ({}). sort ({ 'zid': -1}). Exec (). then (function (d) {var newc = new country ({zn: data.googledata.country, zid: d.zid + 1}); Return newc.save ();})}}}}
Comments
Post a Comment