简体   繁体   English

cordova Javascript require('fs')正在停止我的代码

[英]cordova Javascript require('fs') is stopping my code

I've been working on a mobile app in Visual Studio 2017, I need to do some file access but have struggled for the last week. 我一直在Visual Studio 2017中开发移动应用程序,我需要进行一些文件访问,但上周一直在挣扎。 what i have is: 我所拥有的是:

function findCampaignFolders() {
    alert("in find folder");
    var fs = require('fs');
    alert("finally past file system");
    var Folders = fs.readdirSync("/Campagins");
    var textString = "";
    for (var i = 0; i < Folders.length; i++)
    {
        textString = textString + Folders[i];
    }
    alert(textString);
}

when this function is called alert("in find folder"); 当此函数称为alert("in find folder"); works and sends it promt. 工作并将其发送给promt。

but alert("finally past file system"); 但是alert("finally past file system"); never fires or anything after. 永远不要开火或之后什么。

the cordova is predicted with fs.readdirSync () so it recognized in the VS2017. 科尔多瓦用fs.readdirSync ()预测,因此在VS2017中可以识别。

as for scripts load order in looks like in HTML file: 至于脚本的加载顺序如下所示:

<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="scripts/platformOverrides.js"></script>
<script type="text/javascript" src="scripts/Objects.js"></script> 

Objects.js is where my function lives. Objects.js是我的函数所在的地方。

If anyone can at least give me a hint as why i can't get this working or if there is another way i can go about it it would be a great help. 如果有人至少可以给我一个提示,说明为什么我无法正常工作,或者有其他方法可以解决这个问题,那将是很大的帮助。

var fs = require('fs'); is Node.js code that doesn't work on Cordova apps. 是在Cordova应用程序上无法使用的Node.js代码。 Node.js code is meant to be used on a server or on CLI apps, but not in client apps such as Cordova. Node.js代码旨在在服务器或CLI应用程序上使用,但不能在Cordova等客户端应用程序中使用。

To read/write files in Cordova you have to use cordova-plugin-file https://github.com/apache/cordova-plugin-file 要在Cordova中读取/写入文件,您必须使用cordova-plugin-file https://github.com/apache/cordova-plugin-file

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

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