首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >未捕获TypeError: net.Socket不是构造函数

未捕获TypeError: net.Socket不是构造函数
EN

Stack Overflow用户
提问于 2021-10-13 21:09:42
回答 1查看 108关注 0票数 1

我使用browserify让我在javascript代码中使用require(),但是它生成的代码包含net.Socket(),这给出了未捕获的TypeError: net.Socket不是一个构造函数。在浏览器中有什么解决方案吗?节点版本v14.17.4 browserify 17.0.0这是我的主要代码

tryjson.js

代码语言:javascript
运行
复制
const axios = require('axios');
const InsertToDataBase = require("./InsertToDataBase");
const username = 'admin'
const password = 'admin'
const token = Buffer.from(`${username}:${password}`, 'utf8').toString('base64')
const urlLAMP_0 = 'http://127.0.0.1:8282/~/mn-cse/mn-name/LAMP_0/DATA/la'
const urlLAMP_1 = 'http://localhost:8282/~/mn-cse/mn-name/LAMP_1/DATA/la'
function getDataLAMP_0(){
  axios.get(urlLAMP_0, {
    headers: {
      'Access-Control-Allow-Credentials': 'true',
      'Access-Control-Allow-Origin':'*',
      "X-M2M-RI":"OM2M-webpage",
      'Authorization': `Basic ${token}`,
      'Accept': 'application/json',
      'mode': 'cors',
      'credentials': 'include',
      }
  })
  .then(function(response) {
        document.getElementById("rn0").textContent = response.data['m2m:cin'].rn;
        document.getElementById("ty0").textContent = response.data['m2m:cin'].ty;
        document.getElementById("ri0").textContent = response.data['m2m:cin'].ri;
        document.getElementById("pi0").textContent = response.data['m2m:cin'].pi;
        document.getElementById("ct0").textContent = response.data['m2m:cin'].ct;
        document.getElementById("lt0").textContent = response.data['m2m:cin'].lt;
        document.getElementById("st0").textContent = response.data['m2m:cin'].st;
        document.getElementById("cnf0").textContent = response.data['m2m:cin'].cnf;
        document.getElementById("cs0").textContent = response.data['m2m:cin'].cs;
        document.getElementById("con0").textContent = response.data['m2m:cin'].con;
        return response;
  })
  .then((response) => {
    var rn0 = response.data["m2m:cin"].rn;
    var ty0 = response.data["m2m:cin"].ty;
    InsertToDataBase.insertdatatolamp0(0,1);
  })
}


function getDataLAMP_1(){
  axios.get(urlLAMP_1, {
    headers: {
      'Access-Control-Allow-Credentials': 'true',
      'Access-Control-Allow-Origin':'*',
      "X-M2M-RI":"OM2M-webpage",
      'Authorization': `Basic ${token}`,
      'Accept': 'application/json',
      'mode': 'cors',
      'credentials': 'include',
      }
  })

  .then(function(response) {
        document.getElementById("rn1").textContent = response.data['m2m:cin'].rn;
        document.getElementById("ty1").textContent = response.data['m2m:cin'].ty;
        document.getElementById("ri1").textContent = response.data['m2m:cin'].ri;
        document.getElementById("pi1").textContent = response.data['m2m:cin'].pi;
        document.getElementById("ct1").textContent = response.data['m2m:cin'].ct;
        document.getElementById("lt1").textContent = response.data['m2m:cin'].lt;
        document.getElementById("st1").textContent = response.data['m2m:cin'].st;
        document.getElementById("cnf1").textContent = response.data['m2m:cin'].cnf;
        document.getElementById("cs1").textContent = response.data['m2m:cin'].cs;
        document.getElementById("con1").textContent = response.data['m2m:cin'].con;
  })
}
getDataLAMP_0();
getDataLAMP_1();
setInterval(getDataLAMP_0,100);
setInterval(getDataLAMP_1,100);

InsertToDataBase.js

代码语言:javascript
运行
复制
const {Client} = require('pg')
const client = new Client({
    user:"postgres",
    password:"admin",
    host:"localhost",
    port:"5432",
    database:"postgres",
})
function insertdatatolamp0(rn0,ty0){
client.connect()
.then(()=>console.log("connected successfuly"))
.then(()=>client.query("insert into lamp0 values ($1,$2)",[rn0,ty0]))
.catch(e=> console.log(e))
.finally(()=> client.end())
}
module.exports = { insertdatatolamp0 };

EN

Stack Overflow用户

发布于 2021-10-14 16:23:39

如果我理解正确的话,您有两个端点服务于json数据,然后您将向postgres数据库提供这些数据。

但是,InsertToDataBase.js尝试在浏览器上使用postgre sql驱动程序,因此出现了您看到的错误。

该代码可以在节点上正常运行,但不能在浏览器上运行。

由于tryjson.js似乎提供html文档,而使用json端点,因此您要么放弃数据库部分,要么创建一个小型节点服务器来为您处理这些插入。

票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69562365

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档