简体   繁体   中英

Save the variable in a JSON file

I am very new in Javascript and trying to learn by experiencing it. Sorry if this question is a multiplication or super-easy one. I am using an open source server and I need to make some additions.. following is the part of code:

    /**
 * Event listener
 *
 * @param {SceneJS.node} hit Selected SceneJS node
 */
pick: function(hit) {
    this.unselect();
    this.highlighted = this.SYSTEM.scene.findNode(hit.nodeId);
    var groupId = this.highlighted.findParentByType("translate").data.groupId;
    strID = (hit.nodeId);   
    alert(strID);
........

I defined the strID variable as a global. In the final use, I select a node of a 3D model in browser, and the nodeID of the selected element is appeared on screen as an alert. How can I write the selected entity's "nodeId" (or "strID" variable) in a JSON file, and save the file in a folder in local drive. So I can use this JSON file for further steps.

Maybe I don't know which keywords to search.. I checked couple of ways like JSONstringify but couldn't succeed. Any helps are appreciated.

You should check localStorage, to save this data between sessions.

localStorage.setItem('strID', strID);

and

strID = localStorage.getItem('strID');

Sorry, I didn't read about node, but lets try it:

    /**
 * Event listener
 *
 * @param {SceneJS.node} hit Selected SceneJS node
 */
pick: function(hit) {
    this.unselect();
    this.highlighted = this.SYSTEM.scene.findNode(hit.nodeId);
    var groupId = this.highlighted.findParentByType("translate").data.groupId;
    strID = (hit.nodeId);   
    alert(strID);
    var fs = require('fs');
    fs.writeFile("/data/strID", strID, function(err) {
    if(err) {
        return console.log(err);
    }

    console.log("The file was saved!");
    });
........

To read it use readFile. It it is giving problems about the data format of strID, use JSON-JS to transform to readable before saving

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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