admin管理员组

文章数量:1424936

code:

const request = require('request');
const jimp = require('jimp');

function createShop() {
    Promise.all([
            request({
                uri:"",
                headers: {
                    "x-api-key":"api-key"
                }
            }),
            jimp.read("./Images/shopBack.jpg"),
            jimp.read("./Images/Rarities/unmon.png"),
            jimp.read("./Images/Rarities/rare.png"),
            jimp.read("./Images/Rarities/epic.png"),
            jimp.read("./Images/Rarities/legendary.png"),
            jimp.read("./Images/backdrop.png"),
            jimp.loadFont("./Fonts/Burbank32.fnt"),
            jimp.loadFont("./Fonts/Burbank28.fnt"),
            jimp.loadFont("./Fonts/Burbank60.fnt"),
            jimp.loadFont("./Fonts/Burbank96.fnt"),
        ])
        .then(values => {
            let [, shopImage, unmonImage, rareImage, epicImage, legendaryImage, backDrop, font32, font28, titleFont, mainTitle] = values;
            let imageMap = {unmon: unmonImage, rare: rareImage, epic: epicImage, legendary: legendaryImage};
            let shop = JSON.parse(values[0]);
            let daily = shop.data.daily;
            let featured = shop.data.featured;
            let dailyImgs = Promise.all(daily.map(obj => jimp.read(obj.images.icon)));
            let featImgs = Promise.all(featured.map(obj => jimp.read(obj.images.icon)));
            return Promise.all([
                    dailyImgs,
                    featImgs,
                    values,
                    daily,
                    featured,
                    imageMap
                ]);
        })
        .then(shopImages => {
            let [dailyImgs, featImgs, [, shopImage,,,,, backDrop, font32, font28, titleFont, mainTitle], daily, featured, imageMap] = shopImages;

            let i = 0;
            let iconSize = 225;
            let rarityDiff = 25;
            let xPad = 100;
            let yPad = 250;
            let columns = 3;
            let infoBoxHeight = 80;
            let width = shopImage.bitmap.width, height = shopImage.bitmap.height;

            let Title = "Fortnite Battle Royale Item Shop";
            let titleWidth = measureText(mainTitle, Title);
            let dailyWidth = measureText(titleFont, "Daily Items");
            let featuredWidth = measureText(titleFont, "Featured Items");
            shopImage.print(mainTitle, Math.floor(width / 2 - titleWidth / 2), 50, Title)
                .print(titleFont, Math.floor((iconSize*columns + rarityDiff*3 + xPad) / 2 - dailyWidth / 2), yPad-65, "Daily Items")
                .print(titleFont, Math.floor(width-(iconSize*columns + rarityDiff*3 + xPad)  / 2 - featuredWidth / 2), yPad-65, "Featured Items");


            dailyImgs.map(x => {
                let itemNameWidth = measureText(font32, daily[i].name);
                let itemPriceWidth = measureText(font28, daily[i].price + " V-Bucks");
                x.resize(iconSize, iconSize);
                let infoBox = backDrop.clone()
                    .resize(iconSize, infoBoxHeight)
                    .print(font32, Math.floor(iconSize / 2 - itemNameWidth / 2), 5, daily[i].name)
                    .print(font28, Math.floor(iconSize / 2 - itemPriceWidth / 2), 40, daily[i].price + " V-Bucks");
                let rarityImage = imageMap[daily[i].rarity]
                    .clone()
                    .resize(iconSize, iconSize)
                    posite(x, 0, 0)
                    posite(infoBox, 0, iconSize-infoBoxHeight);
                shopImageposite(rarityImage, (i%columns)*(iconSize+rarityDiff)+xPad, Math.floor(i/columns)*(iconSize+rarityDiff)+yPad);
                i++;
            });

            i = 0;
            featImgs.map(x => {
                let itemNameWidth = measureText(font32, featured[i].name);
                let itemPriceWidth = measureText(font28, featured[i].price + " V-Bucks");
                x.resize(iconSize, iconSize);
                let infoBox = backDrop.clone()
                    .resize(iconSize, infoBoxHeight)
                    .print(font32, Math.floor(iconSize / 2 - itemNameWidth / 2), 5, featured[i].name)
                    .print(font28, Math.floor(iconSize / 2 - itemPriceWidth / 2), 40, featured[i].price + " V-Bucks");
                let rarityImage = imageMap[featured[i].rarity]
                    .clone()
                    .resize(iconSize, iconSize)
                    posite(x, 0, 0)
                    posite(infoBox, 0, iconSize-infoBoxHeight);
                shopImageposite(rarityImage, width-((i%columns)*(iconSize+rarityDiff)+xPad*2+(iconSize+rarityDiff)/2), Math.floor(i/columns)*(iconSize+rarityDiff)+yPad);
                i++;
            });
            shopImage.quality(100)
                .write("./Shop.jpg");
            console.log(chalk.blue("Successfully created and saved shop image to ./Shop.jpg"));
        });
}

this is what i should get:

{"status":200,"data":{"date":"2018-06-03T00:00:00.526Z","featured":[{"id":"5b0e947cdb94f1f04cc0a8e8","name":"Rapscallion","price":"1,500","priceIcon":"vbucks","priceIconLink":".png","images":{"icon":".png","png":".png","gallery":".jpg","featured":".png"},"rarity":"epic","type":"outfit","readableType":"Outfit"},{"id":"5b0e9467db94f14a2ac0a8e7","name":"Scoundrel","price":"1,500","priceIcon":"vbucks","priceIconLink":".png","images":{"icon":".png","png":".png","gallery":".jpg","featured":".png"},"rarity":"epic","type":"outfit","readableType":"Outfit"},{"id":"5b0e93d4db94f173b8c0a8de","name":"Starry Flight","price":"800","priceIcon":"vbucks","priceIconLink":".png","images":{"icon":".png","png":".png","gallery":".jpg","featured":".png"},"rarity":"rare","type":"glider","readableType":"Glider"},{"id":"5b0e934bdb94f1dc5dc0a8d9","name":"Nite Owl","price":"800","priceIcon":"vbucks","priceIconLink":".png","images":{"icon":".png","png":".png","gallery":".jpg","featured":".png"},"rarity":"rare","type":"pickaxe","readableType":"Pickaxe"}],"daily":[{"id":"5ab164dcc3a23b4380359484","name":"Breakin'","price":"800","priceIcon":"vbucks","priceIconLink":".png","images":{"icon":".png","png":".png","gallery":".jpg","featured":false},"rarity":"epic","type":"emote","readableType":"Emote"},{"id":"5ae65cf81b8f51dde9a4a2d2","name":"Tidy","price":"500","priceIcon":"vbucks","priceIconLink":".png","images":{"icon":".png","png":".png","gallery":".jpg","featured":false},"rarity":"rare","type":"emote","readableType":"Emote"},{"id":"5ab175b95f957f27504aa516","name":"Brilliant Striker","price":"1,200","priceIcon":"vbucks","priceIconLink":".png","images":{"icon":".png","png":".png","gallery":".jpg","featured":".png"},"rarity":"rare","type":"outfit","readableType":"Outfit"},{"id":"5abcf2199542fb574bda12a4","name":"Midnight Ops","price":"1,200","priceIcon":"vbucks","priceIconLink":".png","images":{"icon":".png","png":".png","gallery":".jpg","featured":".png"},"rarity":"rare","type":"outfit","readableType":"Outfit"},{"id":"5ab17bef5f957f27504aa530","name":"Plunja","price":"800","priceIcon":"vbucks","priceIconLink":".png","images":{"icon":".png","png":".png","gallery":".jpg","featured":".png"},"rarity":"rare","type":"pickaxe","readableType":"Pickaxe"},{"id":"5ab1bbde9116ac5688c6d7ee","name":"Snow Squall","price":"500","priceIcon":"vbucks","priceIconLink":".png","images":{"icon":".png","png":".png","gallery":".jpg","featured":".png"},"rarity":"unmon","type":"glider","readableType":"Glider"}]}}

Error:

UnhandledPromiseRejectionWarning: SyntaxError: Unexpected token o in JSON at position 1
    at JSON.parse (<anonymous>)
    at Promise.all.then.values (C:\Users\\Documents\project\test.js:84:24)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:160:7)

code:

const request = require('request');
const jimp = require('jimp');

function createShop() {
    Promise.all([
            request({
                uri:"https://fnbr.co/api/shop",
                headers: {
                    "x-api-key":"api-key"
                }
            }),
            jimp.read("./Images/shopBack.jpg"),
            jimp.read("./Images/Rarities/unmon.png"),
            jimp.read("./Images/Rarities/rare.png"),
            jimp.read("./Images/Rarities/epic.png"),
            jimp.read("./Images/Rarities/legendary.png"),
            jimp.read("./Images/backdrop.png"),
            jimp.loadFont("./Fonts/Burbank32.fnt"),
            jimp.loadFont("./Fonts/Burbank28.fnt"),
            jimp.loadFont("./Fonts/Burbank60.fnt"),
            jimp.loadFont("./Fonts/Burbank96.fnt"),
        ])
        .then(values => {
            let [, shopImage, unmonImage, rareImage, epicImage, legendaryImage, backDrop, font32, font28, titleFont, mainTitle] = values;
            let imageMap = {unmon: unmonImage, rare: rareImage, epic: epicImage, legendary: legendaryImage};
            let shop = JSON.parse(values[0]);
            let daily = shop.data.daily;
            let featured = shop.data.featured;
            let dailyImgs = Promise.all(daily.map(obj => jimp.read(obj.images.icon)));
            let featImgs = Promise.all(featured.map(obj => jimp.read(obj.images.icon)));
            return Promise.all([
                    dailyImgs,
                    featImgs,
                    values,
                    daily,
                    featured,
                    imageMap
                ]);
        })
        .then(shopImages => {
            let [dailyImgs, featImgs, [, shopImage,,,,, backDrop, font32, font28, titleFont, mainTitle], daily, featured, imageMap] = shopImages;

            let i = 0;
            let iconSize = 225;
            let rarityDiff = 25;
            let xPad = 100;
            let yPad = 250;
            let columns = 3;
            let infoBoxHeight = 80;
            let width = shopImage.bitmap.width, height = shopImage.bitmap.height;

            let Title = "Fortnite Battle Royale Item Shop";
            let titleWidth = measureText(mainTitle, Title);
            let dailyWidth = measureText(titleFont, "Daily Items");
            let featuredWidth = measureText(titleFont, "Featured Items");
            shopImage.print(mainTitle, Math.floor(width / 2 - titleWidth / 2), 50, Title)
                .print(titleFont, Math.floor((iconSize*columns + rarityDiff*3 + xPad) / 2 - dailyWidth / 2), yPad-65, "Daily Items")
                .print(titleFont, Math.floor(width-(iconSize*columns + rarityDiff*3 + xPad)  / 2 - featuredWidth / 2), yPad-65, "Featured Items");


            dailyImgs.map(x => {
                let itemNameWidth = measureText(font32, daily[i].name);
                let itemPriceWidth = measureText(font28, daily[i].price + " V-Bucks");
                x.resize(iconSize, iconSize);
                let infoBox = backDrop.clone()
                    .resize(iconSize, infoBoxHeight)
                    .print(font32, Math.floor(iconSize / 2 - itemNameWidth / 2), 5, daily[i].name)
                    .print(font28, Math.floor(iconSize / 2 - itemPriceWidth / 2), 40, daily[i].price + " V-Bucks");
                let rarityImage = imageMap[daily[i].rarity]
                    .clone()
                    .resize(iconSize, iconSize)
                    .posite(x, 0, 0)
                    .posite(infoBox, 0, iconSize-infoBoxHeight);
                shopImage.posite(rarityImage, (i%columns)*(iconSize+rarityDiff)+xPad, Math.floor(i/columns)*(iconSize+rarityDiff)+yPad);
                i++;
            });

            i = 0;
            featImgs.map(x => {
                let itemNameWidth = measureText(font32, featured[i].name);
                let itemPriceWidth = measureText(font28, featured[i].price + " V-Bucks");
                x.resize(iconSize, iconSize);
                let infoBox = backDrop.clone()
                    .resize(iconSize, infoBoxHeight)
                    .print(font32, Math.floor(iconSize / 2 - itemNameWidth / 2), 5, featured[i].name)
                    .print(font28, Math.floor(iconSize / 2 - itemPriceWidth / 2), 40, featured[i].price + " V-Bucks");
                let rarityImage = imageMap[featured[i].rarity]
                    .clone()
                    .resize(iconSize, iconSize)
                    .posite(x, 0, 0)
                    .posite(infoBox, 0, iconSize-infoBoxHeight);
                shopImage.posite(rarityImage, width-((i%columns)*(iconSize+rarityDiff)+xPad*2+(iconSize+rarityDiff)/2), Math.floor(i/columns)*(iconSize+rarityDiff)+yPad);
                i++;
            });
            shopImage.quality(100)
                .write("./Shop.jpg");
            console.log(chalk.blue("Successfully created and saved shop image to ./Shop.jpg"));
        });
}

this is what i should get:

{"status":200,"data":{"date":"2018-06-03T00:00:00.526Z","featured":[{"id":"5b0e947cdb94f1f04cc0a8e8","name":"Rapscallion","price":"1,500","priceIcon":"vbucks","priceIconLink":"https://image.fnbr.co/price/icon_vbucks.png","images":{"icon":"https://image.fnbr.co/outfit/5b0e947cdb94f1f04cc0a8e8/icon.png","png":"https://image.fnbr.co/outfit/5b0e947cdb94f1f04cc0a8e8/png.png","gallery":"https://image.fnbr.co/outfit/5b0e947cdb94f1f04cc0a8e8/gallery.jpg","featured":"https://image.fnbr.co/outfit/5b0e947cdb94f1f04cc0a8e8/featured.png"},"rarity":"epic","type":"outfit","readableType":"Outfit"},{"id":"5b0e9467db94f14a2ac0a8e7","name":"Scoundrel","price":"1,500","priceIcon":"vbucks","priceIconLink":"https://image.fnbr.co/price/icon_vbucks.png","images":{"icon":"https://image.fnbr.co/outfit/5b0e9467db94f14a2ac0a8e7/icon.png","png":"https://image.fnbr.co/outfit/5b0e9467db94f14a2ac0a8e7/png.png","gallery":"https://image.fnbr.co/outfit/5b0e9467db94f14a2ac0a8e7/gallery.jpg","featured":"https://image.fnbr.co/outfit/5b0e9467db94f14a2ac0a8e7/featured.png"},"rarity":"epic","type":"outfit","readableType":"Outfit"},{"id":"5b0e93d4db94f173b8c0a8de","name":"Starry Flight","price":"800","priceIcon":"vbucks","priceIconLink":"https://image.fnbr.co/price/icon_vbucks.png","images":{"icon":"https://image.fnbr.co/glider/5b0e93d4db94f173b8c0a8de/icon.png","png":"https://image.fnbr.co/glider/5b0e93d4db94f173b8c0a8de/png.png","gallery":"https://image.fnbr.co/glider/5b0e93d4db94f173b8c0a8de/gallery.jpg","featured":"https://image.fnbr.co/glider/5b0e93d4db94f173b8c0a8de/featured.png"},"rarity":"rare","type":"glider","readableType":"Glider"},{"id":"5b0e934bdb94f1dc5dc0a8d9","name":"Nite Owl","price":"800","priceIcon":"vbucks","priceIconLink":"https://image.fnbr.co/price/icon_vbucks.png","images":{"icon":"https://image.fnbr.co/pickaxe/5b0e934bdb94f1dc5dc0a8d9/icon.png","png":"https://image.fnbr.co/pickaxe/5b0e934bdb94f1dc5dc0a8d9/png.png","gallery":"https://image.fnbr.co/pickaxe/5b0e934bdb94f1dc5dc0a8d9/gallery.jpg","featured":"https://image.fnbr.co/pickaxe/5b0e934bdb94f1dc5dc0a8d9/featured.png"},"rarity":"rare","type":"pickaxe","readableType":"Pickaxe"}],"daily":[{"id":"5ab164dcc3a23b4380359484","name":"Breakin'","price":"800","priceIcon":"vbucks","priceIconLink":"https://image.fnbr.co/price/icon_vbucks.png","images":{"icon":"https://image.fnbr.co/emote/5ab164dcc3a23b4380359484/icon.png","png":"https://image.fnbr.co/emote/5ab164dcc3a23b4380359484/png.png","gallery":"https://image.fnbr.co/emote/5ab164dcc3a23b4380359484/gallery.jpg","featured":false},"rarity":"epic","type":"emote","readableType":"Emote"},{"id":"5ae65cf81b8f51dde9a4a2d2","name":"Tidy","price":"500","priceIcon":"vbucks","priceIconLink":"https://image.fnbr.co/price/icon_vbucks.png","images":{"icon":"https://image.fnbr.co/emote/5ae65cf81b8f51dde9a4a2d2/icon.png","png":"https://image.fnbr.co/emote/5ae65cf81b8f51dde9a4a2d2/png.png","gallery":"https://image.fnbr.co/emote/5ae65cf81b8f51dde9a4a2d2/gallery.jpg","featured":false},"rarity":"rare","type":"emote","readableType":"Emote"},{"id":"5ab175b95f957f27504aa516","name":"Brilliant Striker","price":"1,200","priceIcon":"vbucks","priceIconLink":"https://image.fnbr.co/price/icon_vbucks.png","images":{"icon":"https://image.fnbr.co/outfit/5ab175b95f957f27504aa516/icon.png","png":"https://image.fnbr.co/outfit/5ab175b95f957f27504aa516/png.png","gallery":"https://image.fnbr.co/outfit/5ab175b95f957f27504aa516/gallery.jpg","featured":"https://image.fnbr.co/outfit/5ab175b95f957f27504aa516/featured.png"},"rarity":"rare","type":"outfit","readableType":"Outfit"},{"id":"5abcf2199542fb574bda12a4","name":"Midnight Ops","price":"1,200","priceIcon":"vbucks","priceIconLink":"https://image.fnbr.co/price/icon_vbucks.png","images":{"icon":"https://image.fnbr.co/outfit/5abcf2199542fb574bda12a4/icon.png","png":"https://image.fnbr.co/outfit/5abcf2199542fb574bda12a4/png.png","gallery":"https://image.fnbr.co/outfit/5abcf2199542fb574bda12a4/gallery.jpg","featured":"https://image.fnbr.co/outfit/5abcf2199542fb574bda12a4/featured.png"},"rarity":"rare","type":"outfit","readableType":"Outfit"},{"id":"5ab17bef5f957f27504aa530","name":"Plunja","price":"800","priceIcon":"vbucks","priceIconLink":"https://image.fnbr.co/price/icon_vbucks.png","images":{"icon":"https://image.fnbr.co/pickaxe/5ab17bef5f957f27504aa530/icon.png","png":"https://image.fnbr.co/pickaxe/5ab17bef5f957f27504aa530/png.png","gallery":"https://image.fnbr.co/pickaxe/5ab17bef5f957f27504aa530/gallery.jpg","featured":"https://image.fnbr.co/pickaxe/5ab17bef5f957f27504aa530/featured.png"},"rarity":"rare","type":"pickaxe","readableType":"Pickaxe"},{"id":"5ab1bbde9116ac5688c6d7ee","name":"Snow Squall","price":"500","priceIcon":"vbucks","priceIconLink":"https://image.fnbr.co/price/icon_vbucks.png","images":{"icon":"https://image.fnbr.co/glider/5ab1bbde9116ac5688c6d7ee/icon.png","png":"https://image.fnbr.co/glider/5ab1bbde9116ac5688c6d7ee/png.png","gallery":"https://image.fnbr.co/glider/5ab1bbde9116ac5688c6d7ee/gallery.jpg","featured":"https://image.fnbr.co/glider/5ab1bbde9116ac5688c6d7ee/featured.png"},"rarity":"unmon","type":"glider","readableType":"Glider"}]}}

Error:

UnhandledPromiseRejectionWarning: SyntaxError: Unexpected token o in JSON at position 1
    at JSON.parse (<anonymous>)
    at Promise.all.then.values (C:\Users\\Documents\project\test.js:84:24)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:160:7)
Share Improve this question edited Jun 3, 2018 at 5:53 Max von Hippel 2,9703 gold badges30 silver badges47 bronze badges asked Jun 3, 2018 at 5:32 KaneKane 331 gold badge1 silver badge5 bronze badges 6
  • 1 Don't add meaningless text to get around the warning that you need to add more details - please actually add details instead (such as where the error occurs in the code, to start with) – CertainPerformance Commented Jun 3, 2018 at 5:33
  • Can you provide a Minimal, Complete, and Verifiable example? Also, what have you tried so far to fix the problem? And is the issue that you get the error, or that you don't get the expected output, or both? Please provide less code and more English. Thanks! – Max von Hippel Commented Jun 3, 2018 at 5:37
  • I dont really know what else i can add :/ – Kane Commented Jun 3, 2018 at 6:21
  • console.log(values[0]) returns Request { domain: null, _events: { pipe: [Function] }, _eventsCount: 1, _maxListeners: undefined, uri:.... – Kane Commented Jun 3, 2018 at 7:00
  • Can you print the values, values[0] and JSON.parse(values[0]) on console and provide the output? – Sreehari Commented Jun 3, 2018 at 7:27
 |  Show 1 more ment

2 Answers 2

Reset to default 1

Request method uses callback method. I guess, that is the reason you are not able to print your data.

Please use the module request-promise instead of request. I believe that should solve your issue.

var rp = require('request-promise');

rp({
            uri:"https://fnbr.co/api/shop",
            headers: {
                "x-api-key":"api-key"
            }
        }),
        jimp.read("./Images/shopBack.jpg"),
        jimp.read("./Images/Rarities/unmon.png"),
        jimp.read("./Images/Rarities/rare.png"),
        jimp.read("./Images/Rarities/epic.png"),
        jimp.read("./Images/Rarities/legendary.png"),
        jimp.read("./Images/backdrop.png"),
        jimp.loadFont("./Fonts/Burbank32.fnt"),
        jimp.loadFont("./Fonts/Burbank28.fnt"),
        jimp.loadFont("./Fonts/Burbank60.fnt"),
        jimp.loadFont("./Fonts/Burbank96.fnt"),
    ])
    .then(values => {
        console.log('values of zero is', values[0]);
        let [, shopImage, unmonImage, rareImage, epicImage, legendaryImage, backDrop, font32, font28, titleFont, mainTitle] = values;
        let imageMap = {unmon: unmonImage, rare: rareImage, epic: epicImage, legendary: legendaryImage};
        let shop = JSON.parse(values[0]);
        let daily = shop.data.daily;
        .....

To debug this, start by console.log ing the values array to find out what it is rather than JSON like you expected.

本文标签: