Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/448.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
代码上的Javascript小胡子模板无效_Javascript_Node.js_Mustache - Fatal编程技术网

代码上的Javascript小胡子模板无效

代码上的Javascript小胡子模板无效,javascript,node.js,mustache,Javascript,Node.js,Mustache,我正在开发一个应用程序,我已经通过npm安装了mustache 然后在main.js文件中,我将其导入如下: const Mustache = require('mustache') 然后,我有一个html模板文件,其中包含以下内容: {{title}}位于html文件的平铺部分 使用node,我加载了模板文件并尝试运行render 代码如下: fs.readFile('template.html', (err, data) => { var mydata = Mustache.r

我正在开发一个应用程序,我已经通过npm安装了mustache

然后在main.js文件中,我将其导入如下:

const Mustache = require('mustache')
然后,我有一个html模板文件,其中包含以下内容:

{{title}}
位于html文件的平铺部分

使用node,我加载了模板文件并尝试运行render

代码如下:

fs.readFile('template.html', (err, data) => {

  var mydata = Mustache.render(data, {title: "sometitle"});

  fs.writeFile('result.html', mydata, (err) => {

    if (err) throw err;
    console.log('The file has been saved!');

  });

}); 
我不断地发现这个错误:

类型错误:模板无效!模板应为“字符串”,但“对象”作为mustache#render的第一个参数给出

如何修复此问题,以便更改和保存结果?

如您所见:如果不指定编码,则回调将接收原始缓冲区;如果您想要一个字符串,只需添加一个编码(假设该文件为UTF-8格式):


在尝试编解码器后仍会出现相同的错误。是否验证“数据”的类型?
fs.readFile('template.html', 'utf8', (err, data) => {
  var mydata = Mustache.render(data, {title: "sometitle"});
  fs.writeFile('result.html', mydata, (err) => {
    if (err) throw err;
      console.log('The file has been saved!');
  });
});