简体   繁体   English

如何使用Webtask存储获取和设置JSON数据?

[英]How to get and set JSON data with Webtask storage?

Trying to figure out how to save mouse coordinates to Webtask storage after reading this post: https://blog.codepen.io/2018/05/04/say-you-need-to-get-set-some-json-data-from-a-pen/ 阅读这篇文章后试图弄清楚如何将鼠标坐标保存到Webtask存储中: https ://blog.codepen.io/2018/05/04/say-you-need-to-get-set-some-json-data -从笔/

I want to create a heatmap that will write mouse coordinates to Webtask storage when user clicks inside a div. 我想创建一个热图,当用户在div中单击时,将鼠标坐标写入Webtask存储。 And loads the updated data on page load. 并在页面加载时加载更新的数据。

This is what I got so far 这就是我到目前为止

 const webtask_url = `https://wt-944eb9b4c90e6c80852f9671db4a1404-0.sandbox.auth0-extend.com/heatmap`; var canvas = document.querySelector("canvas"); var ctx=canvas.getContext("2d"); var style = getComputedStyle(document.body); canvas.width = parseInt(style.getPropertyValue('--s'), 10); canvas.height = parseInt(style.getPropertyValue('--s'), 10); //fill canvas ctx.fillStyle = style.getPropertyValue('--c1'); ctx.fillRect(0, 0, canvas.width, canvas.height); var mouse = { x: undefined, y: undefined } //get mouse position inside the div function getMousePos(canvas, evt) { var rect = canvas.getBoundingClientRect(), // abs. size of element scaleX = canvas.width / rect.width, // relationship bitmap vs. element for X scaleY = canvas.height / rect.height; // relationship bitmap vs. element for Y return { x: (evt.clientX - rect.left) * scaleX, // scale mouse coordinates after they have y: (evt.clientY - rect.top) * scaleY // been adjusted to be relative to element } } //click event window.addEventListener("click", function(event){ mouse.x = getMousePos(canvas, event).x; mouse.y = getMousePos(canvas, event).y; drawPoint(mouse.x, mouse.y); let data = { "x": mouse.x, "y": mouse.y }; //trying to send the coordinates to Webtask fetch(webtask_url, { method: 'post', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data) }) .then(response => response.json()) .then(data => console.log('data is', data)) }); //draw a circle function drawPoint(x, y) { ctx.beginPath(); ctx.shadowBlur=10; ctx.shadowColor="rgb(233, 30, 99)"; ctx.arc(x,y,5,0,2*Math.PI); ctx.fillStyle = style.getPropertyValue('--c2'); ctx.fill(); } //load coordinates from Webtask storage fetch(webtask_url) .then(response => response.json()) .then(data => { data.forEach(function(item, i) { drawPoint(item.x, item.y); }); }); 
 :root { --bg: whitesmoke; --c1: #5E35B1; --c2: #E91E63; --s: 300px; --shadow: 0 1em 5em -1em rgba(94, 53, 177, 0.8); } *{ margin: 0; padding: 0; border: 0; box-sizing: border-box; } html,body { height: 100%; width: 100%; } body { background: var(--bg); font-family: "Open Sans", sans-serif; display: grid; } #canvas { display: grid; margin: auto; box-shadow: var(--shadow); } 
 <canvas id="canvas"></canvas> 

Webtask side Web任务方面

Author is updating color field in the Storage. 作者正在更新存储中的颜色字段。 My problem is that I am completely lost here. 我的问题是我在这里完全迷路了。 I understand I have to adjust ctx.storage.set part to accept my coordinates but I don't want to update them, I want to create a new entry in my JSON. 我知道我必须调整ctx.storage.set部分以接受我的坐标,但是我不想更新它们,我想在JSON中创建一个新条目。

module.exports = function(ctx, cb) {

 if (Object.keys(ctx.query).length !== 0) {
   ctx.storage.set(ctx.query, { force: 1 }, function (error) {
      if (error) return cb(error);
      cb(null, { 
        "message": "success" ,
        "color": ctx.query.color
      });
   });
 } else {
   ctx.storage.get(function (error, data) {
      if (error) return cb(error);
      cb(null, data);
   });
 }

}

If you want to append values to a list you'll need to store and manipulate an array instead of an object. 如果要将值追加到列表,则需要存储和操作数组而不是对象。

With ES6 the easiest way to do so is with the spread operator: 使用ES6,最简单的方法是使用传播算子:

newData = [...oldData, newItem]

I've refactored your code a bit. 我已经重构了一下您的代码。

module.exports = (ctx, cb) => {
  ctx.storage.get((error, data) => {
    if (error) return cb(error)
    if (Object.keys(ctx.query).length === 0) {
      cb(null, data)
    } else {
      const newData = [...data, ctx.query]
      ctx.storage.set(newData, error => {
        if (error) return cb(error)
        cb(null, { "message": "success" , "color": ctx.query.color })
      })
    }
  })
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM