javascript - Promise in cron job -


i try run code without success. first call works in start of node.

here current code:

const product = require('../models/product'); const price = require('../models/price');  const cron = require('node-cron'); const amazon = require('amazon-product-api'); const util = require('util'); const _ = require('underscore') /**  * cron job  * tracking price  */  exports.track = () => {  cron.schedule('* * * * *', () => {      const client = amazon.createclient({         awsid: process.env.aws_id,         awssecret: process.env.aws_secret,         associd: process.env.aws_tag     });    promise.all([product.getasin()])     .then(([asin]) => {       let listid = _.pluck(asin, '_id');       let listasin = _.pluck(asin, 'asin');       if (asin.length === 0) {         return       }        client.itemlookup({           idtype: 'asin',           itemid: listasin,           domain: 'webservices.amazon.fr',           responsegroup: 'itemattributes,offerfull,salesrank'       }).then((results) => {         for(i=0; i<listid.length; i++){             results[i].id = listid[i];         }         for(res of results) {           price.addprice({             asin: res.asin[0],             product: res.id,             saleprice: res.offers[0].offer[0].offerlisting[0].price[0].amount[0],           })         }         console.log(listid);         product.makeupdate(listid);        }).catch(function(err) {           console.log(err);           console.log(util.inspect(err, true, null));       });     })     .catch((err) => {       console.log(err);     }) }) } 

requests mongodb asynchronous.

product

const mongoose = require('mongoose'); mongoose.promise = promise; const _ = require('underscore'); const moment = require('moment');  const productschema = new mongoose.schema({   name: string,   domain: string,   originurl: { type: string, unique: true },   check: { type: number, default: 0 },   ean: string,   asin: string }, { timestamps: true });  object.assign(productschema.statics, {   getasin() {     return this.find(       { updatedat: { $lt: onemin },         asin: { $ne: null }       }     ).limit(10)     .select({ asin: 1 })     .exec()//.then((tuples) => _.pluck(tuples, 'asin'))   },   makeupdate(id) {     console.log('list des id updated => ' + id);     return this.update({ _id: { $in: id } }, { $inc : { "check": 1 } } , {multi: true}).exec();   } });  const product = mongoose.model('product', productschema);  module.exports = product;  const onemin = moment().subtract(1, 'minutes').format(); 

also, since i'm absolutely new javascript , node.js in general, best practices or general tips appreciated! :)


Comments