【问题标题】:Protractor : Reading a json. "Error - Failed: The "path" argument must be one of type string, Buffer, or URL. Received type function"量角器:读取 json。 “错误 - 失败:“路径”参数必须是字符串、缓冲区或 URL 类型之一。接收到的类型函数”
【发布时间】:2019-10-06 22:27:14
【问题描述】:

我正在尝试读取 json 文件。但我得到了一个错误。

基本上有2个文件——一个是readjson.js(json阅读器模块),另一个是量角器文件本身

readjson.js 文件

=============================================

var jsonreader = function () {


this.parseJSON = function (filename){

    let rawdata = fs.readFile (filename, 'utf8', function (err,contents){

        let data = JSON.parse(contents);
        return (data);
    })

};

};

module.exports = jsonreader;

===================================

量角器spec.js文件

================================================ ========

let jsonreader = require ('./readjson.js');

var readjson = new jsonreader();


describe ('Protractor test json', function (){


browser.ignoreSynchronization = true;


it ("reading json ", () =>{
    browser.manage().timeouts().implicitlyWait(30000);
    var filename = 'user.json';


    readjson.parseJSON(filename,  function (data) {

        console.log ("value is " + data)
    });

})
})

================================================ ====

当我运行它时,错误“错误 - 失败:“路径”参数必须是字符串、缓冲区或 URL 类型之一。接收类型函数”出现在这一行 "console.log("值为 " + data)"

我无法解析 json 文件。我做错了什么?

谢谢

【问题讨论】:

  • 你可以直接使用const jsonData = require('./readjson.js');来获取json文件,不需要为此使用fs模块。

标签: json protractor


【解决方案1】:

我找到了解决方案。

基本上我为数据分配了一个全局变量。

var data = readmodule.parseJSON (filename)

然后如果我想使用它

data.then (function(userdata){

   console.log (userdata.name)
});

【讨论】:

    猜你喜欢
    • 2019-12-23
    • 2020-08-22
    • 2019-08-15
    • 2019-10-13
    • 2020-07-21
    • 2021-08-18
    • 2020-11-22
    • 1970-01-01
    • 2020-06-20
    相关资源
    最近更新 更多